I have a command
sed -e 's/\\N//g' ${OUTPUT_DIR}/${OUTPUT_SED_FILE_NAME} > ${OUTPUT_DIR}/${OUTPUT_FILE_NAME}
I want to remove the header and footer from the source file. What switch I need to put in the sed line.
Thanks,
Is this what you're trying to do?
$ seq 5 | sed '1d; $d'
2
3
4
Related
test.txt contains:
this is a line
another line
one more line
For example, this can remove 1-2 lines, and save the rest into the rest.txt
sed -e '1,2d' test.txt > rest.txt
But the original file remains intact. Then how to get the remaining lines from the file? In this example, I want to remove the first 2 lines, save them into a file 'deleted.txt', and save the 3rd line into 'rest.txt'.
With GNU sed:
seq 1 5 | sed -e '1,2w deleted.txt' -e '1,2d' > rest.txt
w filename: Write the current pattern space to filename.
awk 'NR<=2{print $0 > "deleted.txt"}NR>2{print $0 > "rest.txt"}' test.txt
For lines(here NR) <= 2 redirect the output to deleted.
For lines(here NR) > 2 redirect the output to rest.txt.
Use the –e option to include 2 replace commands for an example file:
1 to replace all “erors” with “errors” and
1 to replace all last words with “final.”
What command will do this?
This would work:
sed 's/erors/errors/g;s/last/final/g'
As well as:
sed -e 's/erors/errors/g' -e 's/last/final/g'
In awk you can do
cat file
these are all erors that I have
awk '{sub(/erors/,"errors");$NF="final"}1' file
these are all errors that I final
I have a text file named: "header" and I wish to insert it as the header of "anotherfile" which already contains some text.
The final output should be:
"header" text content
right above
"otherfile" text content
On ubuntu linux system I tried the following, which got me close but not quite:
cat otherfile | sed '1 r header' - > myoutputfile
the problem with this solution is that it enters all the content of "header" one line AFTER the first line of "otherfile", instead of BEFORE the first line of "otherfile".
Is there any neat way to make the content of my "header" file appear as the header of "otherfile" ?
i should mention that the following attempt
cat otherfile | sed '0 r header' - > myoutputfile
fails with the following error message:
sed: -e expression #1, char 3: invalid usage of line address 0
The answer to the question stated in your question is simply:
cat header otherfile > myoutputfile
But to answer the question in your comments: Assuming that cat otherfile is some pipeline, not actually a file, all you need is:
cat otherfile | cat header - > myoutputfile
e.g.:
$ cat file1
a
b
c
$ cat file2
foo
bar
$ cat file1 | cat file2 -
foo
bar
a
b
c
This might work for you (GNU sed):
sed -e '1h;1r header' -e '1d;2H;2g' file
or:
sed '1e cat header' file
ok, this is the solution that works for me:
cat otherfile | sed -e '2{x;G};1{h;rheader' -e 'd}' - > myoutputfile
I have in sample.txt the following content
abc
efg
hij
klm
nop
qrs
I have tried replacing abc with other text with
sed -i '/abc/c\This line is removed by the admin.' sample.txt
Output:
This line is removed by the admin.
efg
hij
klm
nop
qrs
It worked but for a single line.
But I am wondering how could I replace a given set of lines say 1 to 3 using sed?
If you know the line numbers, you prepend them to your pattern, like so:
sed -i '4 s/abc/c\This line is removed by the admin./' sample.txt
The above will change line 4. If you want to change ranges (say, lines 5-10), enter the start and end line numbers with a comma between:
sed -i '5,10 s/abc/c\This line is removed by the admin./' sample.txt
$ represents the last line in the file so if you wanted say, line 100 to the end:
sed -i '100,$ s/abc/c\This line is removed by the admin./' sample.txt
You may find this link helpful. Look for the section on Ranges by line number.
If your only criterion is the line numbers, then you can specify them like so:
sed -i '1,3 s/.*/This line is removed by the admin./' sample.txt
Here is an awk solution if you like to try:
awk 'NR>=1 && NR<=3 {$0="This line is removed by the admin."}1' file
This line is removed by the admin.
This line is removed by the admin.
This line is removed by the admin.
klm
nop
qrs
To write it back to the file
awk 'NR>=1 && NR<=3 {$0="This line is removed by the admin."}1' file > tmp && mv tmp file
This might work for you (GNU sed):
sed '1,3c\replace lines 1 to 3 with this single line' file
if you want to replace each line within the range use:
sed $'1,3{\\athis replaces the original line\nd}' file
or perhaps more easily:
sed '1,3s/.*/this replaces the original line/' file
I want to delete first and last line from the file
file1 code :
H|ACCT|XEC|1|TEMP|20130215035845|
849002|48|1208004|1
849007|28|1208004|1
T|2
After delete the output should be
849002|48|1208004|1
849007|28|1208004|1
I have tried below method but has to run it 2 times, I want one liner solution to remove both in one go!
sed '1,1d' file1.txt >> file1.out
sed '$d' file1.out >> file2
Please suggest one liner code....
You could use ;
sed '1d; $d' file
Use Command Separator
In sed, you can separate commands using a semicolon. For example:
sed '1d; $d' /path/to/file
How about:
sed '$d' < file1.txt | sed "1d"
Try sed -i '1d;$d' /path/to/file
awk 'NR>2{print v}{v=$0}'
Starting with line 3, print the previous line each time. This means the first and last lines will not be printed.