This question already has an answer here:
-
Combining two sed commands
2 answers
How can I combine the following two sed commands.
One loops all files in a directory and removes the first line from them.
The other removes any double quotes " from the start of file lines.
Remove first line of each file
for each in `/bin/ls -1`;do sed -i 1d $each;done
Beginning of line
for each in `/bin/ls -1`;do sed -i 's/^"//g' $each;done
You can do:
for each in *; do sed -i.bak 's/^"//g; 1d' "$each"; done
You can put them into the same invocation of sed like this:
for f in *; do sed -i '1d;s/^"//' "$f"; done
As well as combining the two sed commands, I have also used a glob *
rather than attempting to parse ls, which is never a good idea.
Also, your substitution needn't be global, as it can by definition only apply to each line once, so I removed the g
modifier as well.
You can do it with a single commmand in this way:
sed -i.bak --separate '1d ; s/^"//' *
Explanation
With --separate
you're telling sed to treat the files separately, the default is to process them as a long single file but you are using adresses (1
in the first command) so the default doesn't work.
'1d ; s/^"//'
just combines the two commands (separated by ;
).
You can use -e
to perform different sed
commands in the same line: sed -e 'command_1' -e 'command_2' ... -e 'command_n'
. You can also use sed 'command_1; command_2; ...; command_n
.
Let's use the first option and loop through the files:
for file in *
do
sed -i.bak -e '1d' -e 's/^"//' "$file"
done
Note also that I use for file in *
, so that *
expands to the files in the current directory. This is better than parsing the output of ls
(Why you shouldn't parse the output of ls(1) is a good read).
Finally, it is a good practise to create a backup file when using -i
, as anubhava suggests. This way, you are always in the safe side :)