Sponsored Content
Top Forums Shell Programming and Scripting sed for delete the 2th 3th 4th Post 302687303 by yanglei_fage on Thursday 16th of August 2012 10:35:00 AM
Old 08-16-2012
can we use one line sed to handle this ?

---------- Post updated at 07:35 AM ---------- Previous update was at 07:32 AM ----------

Quote:
Originally Posted by Don Cragun
Your request is extremely vague. What do you mean by?:

Do you want to delete the 2nd, 3rd, and 4th lines in the file?
Do you want to delete the 2nd, 3rd, and 4th fields in each line in the file?
What is in the lines other than unaccented uppercase letters and unaccented lowercase letters? If the fields are separated from each other by one or more space or tab characters do you also want the space and tab characters following the fields that are being removed to be removed or do you want to leave the separators unchanged?
Do you want to only print lines that have been changed, or do you want to every line after the changes have been completed. (Your current code appears to want to print unchanged lines once and changed lines twice.)

According to the standard, the use of 2g in
Code:
s/[a-zA-Z]*//2gp

produces undefined results.
You can specify a count or the g flag, but not both.
Since you don't show us any of your input, you don't give us the output from the commands you listed, and you don't show us the output you want to get from a representative input file, any suggestions you get here will be wild conjecture as to whether or not you will get anything resembling what you want.

With the sed commands you are showing, I assume that you are trying to remove fields rather than lines, that you want to leave any field separators unchanged, and that you want to every line in the file once. In that case, something like:
Code:
sed 'sed 's/[a-zA-Z][a-zA-Z]*//14
s/[a-zA-Z][a-zA-Z]*//4
s/[a-zA-Z][a-zA-Z]*//3
s/[a-zA-Z][a-zA-Z]*//2' 9

will delete two or more letters in the 2nd, 3rd, 4th, and 14th nonadjacent occurrences of adjacent letters from every line. If a line has fewer occurrences of this condition, it will still make changes to any of the times the request is matched. If the input file (9) contains:
Code:
first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth thirteenth fourteenth fifteenth sixteenth seventeenth eighteenth nineteenth twentieth
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th

the output will be:
Code:
first    fifth sixth seventh eighth ninth tenth eleventh twelfth thirteenth  fifteenth sixteenth seventeenth eighteenth nineteenth twentieth
1st 2 3 4 5th 6th 7th 8th 9th 10th


I want to delete the 2nd, 3rd, and 4th fields in each line in the file, can we use one line sed to handle this?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete particular value from file using 'sed'

Hi, I have two files test1,test2. If the number in test1 file exist in test2 then i want to remove that from test2 file. Ex: File- test1 12 13 14 15 ============== File- test2 1A~12 2B~13 3C~33 4D~22 I want to remove row which contains 12,13 from test2. I am using this sed... (2 Replies)
Discussion started by: sai_nj
2 Replies

2. UNIX for Dummies Questions & Answers

Delete ^ using sed

Hi All, I am very new to UNIX... I was trying to delete ^ from a file and save it with different name... The version is AIX 5.3 The input file is in this directory /a/b The Input file name is o9876.out I want the output file in same path with different name like ABC.txt... (5 Replies)
Discussion started by: us_pokiri
5 Replies

3. Shell Programming and Scripting

delete using sed

hi , i need to delete all the lines in a file except a line which starts with the letter SP (6 Replies)
Discussion started by: mhdmehraj
6 Replies

4. Shell Programming and Scripting

sed-delete

Guys, file1: test \ 123 cat file1 | sed '/test \/d' ---- does not work I have to remove the line "test \" in file1 can somebody help ? (7 Replies)
Discussion started by: giri_luck
7 Replies

5. Shell Programming and Scripting

sed - delete everything until the first digit comes up

Hi, I have a text file with content as follows: bla foo3200492 comment on this: 3900302 here comes the teddy 12 all I need is: 3200492 3900302 12 So I need the correct "sed" command to delete everything until the first number. I am not a regex expert, because I have to... (3 Replies)
Discussion started by: mcW
3 Replies

6. Shell Programming and Scripting

sed delete option

I have tried doing this to delete some lines: sed '1,10d' file Now I want to specify a variable as a line number for example: lastline=wc -l file linestart=$lastline - 20 sed '$linestart,$lastlined' file but this will give error: sed: -e expression #1, char 3: extra characters after... (4 Replies)
Discussion started by: zorrox
4 Replies

7. Shell Programming and Scripting

sed delete

I am not able to analyze the below code.. What I expected is that DELETED will be replaced with the first field in every line.But after giving a try, the output was different. Can anyone pleas analyze and explain the code in detail? Many thanks... $ sed 's/* /DELETED /g' g1.txt > g4.txt... (2 Replies)
Discussion started by: giridhar276
2 Replies

8. Shell Programming and Scripting

Delete data between [] with sed

i'm cat /var/log/message Jan 10 14:48:45 LOG SKYPE-OUT IN=eth1 OUT=eth2 SRC=192.168.1.65 DST=203.157.168.5 PROTO=TCP SPT=1284 DPT=3306 Jan 10 14:48:45 LOG HTTPS IN=eth0 OUT=eth1 SRC=207.46.15.251 DST=192.168.1.47 PROTO=TCP SPT=443 DPT=2069 Jan 10 14:48:45 LOG HTTPS IN=eth0 OUT=eth1... (2 Replies)
Discussion started by: slackman
2 Replies

9. Shell Programming and Scripting

sed to delete ^M

I am trying to use sed to delete the ^M at the end of the line and when i pass a file './asciiFix.sh wintest.txt' I get an error saying sed: can't read : no such file or dir. Can someone give me a pointer as to what I am doing wrong? #!/bin/sh if file "$@" | grep "with CRLF"; then echo... (5 Replies)
Discussion started by: kwatt019
5 Replies

10. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies
All times are GMT -4. The time now is 03:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy