what is the sed command to remove the first two characters of every line of a text file?
each line of the text file has the same amount of characters, and they are ALL NUMERIC. there are hundreds of lines though.
for example,
>cat file1.txt
10081551
10081599
10082234
10082259
20081134
20081159
30082232
10087721
>
I want the end result to remove the first two digits of each line
>cat file2.txt
081551
081599
082234
082259
081134
081159
082232
087721
>
Thank you so much!!
cat input_file | sed 's/^..//' > output_file
sed 's/^..//' file1.txt > file2.txt
man sed to see if your version support -i option.
sed -i 's/\(.\{2\}\)//' file
Or even:
sed 's .\{2\} ' infile
If your sed supports the -r option:
sed -r 's .{2} ' infile
Or:
sed 's .. ' infile
thank you all! I'll probably make a new post, but I actually need the last two characters removed also....
>cat file1.txt
10081551
10081599
10082234
10082259
20081134
20081159
30082232
10087721
>
ends up looking like:
>cat file1.txt
100815
100815
100822
100822
200811
200811
300822
100877
>
sed -e 's/^..//' -e 's/..$//' file1.txt > file2.txt
lewk
June 3, 2009, 2:57am
9
ajp7701:
what is the sed command to remove the first two characters of every line of a text file?
each line of the text file has the same amount of characters, and they are ALL NUMERIC. there are hundreds of lines though.
for example,
>cat file1.txt
10081551
10081599
10082234
10082259
20081134
20081159
30082232
10087721
>
I want the end result to remove the first two digits of each line
>cat file2.txt
081551
081599
082234
082259
081134
081159
082232
087721
>
Thank you so much!!
How could I do the same as ajp7701 but instead of deleting the first two characters of every line, I want to delete the last two characters of every line? E.g:
Before:
>cat file1.txt
10081551
10081599
10082234
After:
>cat file2.txt
100815
100815
100822
See my previous post. The first command removes the first two, the second removes the last two.
lewk
June 4, 2009, 11:04am
11
ahh, I missed that. Thanks so much. I really need to sharpen up my shell skills.
This is not a sed command, but it works also. Try it.
cut -c1-6 file_name |cut -c3-6
This will remove the first 2 and last 2 characters of the datas in the file
Best Regards
Why two cuts? That's the same as:
cut -c3-6 file_name
Only if all the lines are exactly 8 characters long. The sed command will work on any length, or even variable length lines.
lewk
June 5, 2009, 6:42am
14
cfajohnson:
Why two cuts? That's the same as:
cut -c3-6 file_name
Only if all the lines are exactly 8 characters long. The sed command will work on any length, or even variable length lines.
The sed command working on any length/variable length lines is exactly what I looked for. Thx
cfajohnson:
Why two cuts? That's the same as:
cut -c3-6 file_name
Only if all the lines are exactly 8 characters long. The sed command will work on any length, or even variable length lines.
I assumed it was a constant length lines file. In this case you solution is the best one. Thanks for the headsup.
Best Regards
Hi all,
I've been through this post and found great.
I really like the logic of mr.cfajohnson.
I have a Query regarding this post with Mr.cfajohnson
Please help me in understanding this.
as you mentioned two commands
one for the case where you are removing the first two digits
sed 's/^..//' file1.txt > file2.txt
and secondly
sed -e 's/^..//' -e 's/..$//' file1.txt > file2.txt
to remove the last two lines
Now my query is,that while using the sed command, the [two dots] means removing two digits for either of the command. Please confirm
Also if I want to remove the first three digits would it be like
sed 's/^...//' file1.txt > file2.txt
Please Confirm.
Also as i am new to "sed" Plese do guide regarding
sed 's ===============> what does ('s) means here
Similarly,
sed -e ==============> what does (-e) means here.
Thanks in advance.
Replies from anyone is welcome.
panyam
June 9, 2009, 5:16am
17
www.grymoire.com/Unix/Sed.html
Thankz Panyam,
But a little description from anyone of you might help me.
Please try.
panyam
June 9, 2009, 7:59am
19
The sed statement u specified
will replace the first three characters with the string u specified b/w "//" ( in this case it's nothing)
s-> will be used for searching
e-> to concat multiple sed expressions in a single sed line
Dear panyam,
thnkz a lot for tht.
it helped me out.
now please tell me if i want to remove the number from middle
wat changes will be made in this command
file1.txt contains data like
00957890
65948058
89259025
45009894
68409860
98695869
34320040
86598609
now i want the output that the middle two digits will be omitted from the output
and output should be
output.txt
009890
658058
899025
459894
689860
985869
340040
868609
what changes will be made in the sed command. Please be noted that the range is not static (i.e. 8) it may be 10, 11 or 6, 3.