sed for delete the 2th 3th 4th


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed for delete the 2th 3th 4th
# 1  
Old 08-16-2012
sed for delete the 2th 3th 4th

Hi expert

The delete the 2th
Code:
lyang0@lyang0-OptiPlex-755:~/del$ sed 's/[a-zA-Z]*//2p' 9

to delete from 2th
Code:
lyang0@lyang0-OptiPlex-755:~/del$ sed 's/[a-zA-Z]*//2gp' 9

but if I want to delete 2th 3rd 4th fileds

or

but if I want to delete 2th 15th fileds

how could I do?

Last edited by yanglei_fage; 08-16-2012 at 11:33 AM..
# 2  
Old 08-16-2012
Try like...
Code:
 sed '1~4d' test1.txt

# 3  
Old 08-16-2012
Quote:
Originally Posted by yanglei_fage
Hi expert

The delete the 2th
Code:
lyang0@lyang0-OptiPlex-755:~/del$ sed 's/[a-zA-Z]*//2p' 9

to delete from 2th
Code:
lyang0@lyang0-OptiPlex-755:~/del$ sed 's/[a-zA-Z]*//2gp' 9

but if I want to delete 2th 3th 4th

or

but if I want to delete 2th 15th

how could I do?
Your request is extremely vague. What do you mean by?:
Quote:
but if I want to delete 2th 3th 4th
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

# 4  
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?
# 5  
Old 08-16-2012
To delete the 2nd, 3rd, and 4th fields (using the same assumptions I made above about what you define a field to be), the following single line sed will handle it:
Code:
sed '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

it is just harder to read than the same code with the <semicolon>s replaced by <newline>s. It can't be done with a single sed substitute command. (At least it can't in a portable way. Some implementations of sed might support providing a list and/or range of values when specifying which occurrence of a match for the BRE in a substitute command, but I haven't seen any that do so.
# 6  
Old 08-16-2012
The following one sed command seems portable:
Code:
sed 's/\([a-zA-Z]*\) *[a-zA-Z]* *[a-zA-Z]* *[a-zA-Z]*/\1/'

# 7  
Old 08-16-2012
Quote:
Originally Posted by binlib
The following one sed command seems portable:
Code:
sed 's/\([a-zA-Z]*\) *[a-zA-Z]* *[a-zA-Z]* *[a-zA-Z]*/\1/'

The person who started this thread still hasn't told us what the input format is. This one line will remove the 2nd, 3rd, and 4th fields if and only if there are no characters other than spaces and unaccented alphabetic characters in the before the end of the 4th string of letters ends.

The submitter also asked how to delete the 2nd and 15th fields. That can still be done as a single replacement but it is tedious to type and debug at best.

We need a CLEAR statement of what a field is from the submitter before any of us will know if what we might suggest will work.

My script will take the input file:
Code:
1st 2nd 3rd{tab}4th 5th

where {tab} is a tab character and produce:
Code:
1st 2 3{tab}4 5th

With the same input, your script will just copy the input to the output without change. I have no idea which if either of these is what the submitter wants since there is no definition of what is in the file other than some alphabetic characters.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question