SED scipt help - line extraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED scipt help - line extraction
# 1  
Old 03-15-2006
SED scipt help - line extraction

Forgive me if this is a dumb question...I'm a Windows sys admin with little programming knowledge.

I have files containing anywhere from 3 to 200 lines. Using SED, I want to extract only lines containing a specific instance of "ISS." It is possible that "ISS" will occur several times in a line or not, but the only instance of "ISS" I want considered will always be located at, let's say, the 87th character position if you're counting from left to right on the line.

So if "ISS" is located at position 87, remove that instance of "ISS" from the line, then append the line to a new file.

If anyone could help with this I'd be eternally grateful!

-Tony
# 2  
Old 03-15-2006
I am not sure exactly what you want, but maybe this is it...

sed -n 's/^\(.\{86\}\)ISS.*$/\1/p' < inputfile >> anotherfile
# 3  
Old 03-15-2006
No wait, maybe you want this...

sed -n 's/^\(.\{86\}\)ISS\(.*/)$/\1\2/p' < inputfile >> anotherfile
# 4  
Old 03-16-2006
Hi Perderabo,

Thanks very much for your response. I've tried both of your suggestions, and they both output a blank file, though its entirely possible I could be doing something wrong. Was it intentional that the command counts to the 86th character position instead of the 87th?

Thanks,
Tony
# 5  
Old 03-16-2006
No way I'm typing in an example with 90+ characters. Here is one with ISS at position 11 in a couple of lines....
Code:
$ cat data
12345678901234567890
ISS 123   ISS
sjjsISSjjjISS1234
$ sed -n 's/^\(.\{10\}\)ISS.*$/\1/p' < data
ISS 123
sjjsISSjjj
$

Only lines 2 and 3 were output. Line 1 was ignored, no "ISS" at position 11. The early ISS in line 3 is retained. The ISS at position 11 and all characters following that ISS were dropped. The second version would drop only the ISS, so in line 3, that trailing "1234" would move over to position 11.

So it is working exactly like I expected. And as I said, I am only guessing as to what you wanted. I guess that I guessed wrong. Smilie *shrug* You can't win them all.
# 6  
Old 03-16-2006
Hey, I'm thankful you took the time to guess at all! Smilie

I figured out one reason why it didn't work for me...I had to use double quotes before s and after p instead of the single quotes.
Once I switched those out, your first example worked for me.

Your second example is actually closer to what I wanted, but I couldn't get it working. Let me offer some clarification (which I'm sure you'd have appreciated beforehand...sorry! hard for me to think in programmer terms)

After finding "ISS" in position x, that instance should be replaced with 3 empty spaces, and everything following should be retained and written to the new file. All other instances of ISS in that line are retained as well.

I should also mention I'm using sed utilities that have been ported to Windows. The two I'm playing with were found below:

http://users.cybercity.dk/~bse26236/.../SED.HTM#11.80

When I try your second script using this one, I get this error: sed: garbled command s/^\(.\{86\}\)ISS\(.*/)$/\1\2/p


http://sed.sourceforge.net/grabbag/ssed/

With this one (super sed) I get this: ssed: -e expression #1, char 25: unknown option to `s'

Maybe these ported version aren't as good as the real thing? If you have any ideas, I'd greatly appreciate it. And thanks again for your help.
# 7  
Old 03-16-2006
Code:
ruby -ne "print if sub!(/^(.{87})ISS/,'\1   ')" old >new

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extraction problem with sed command

Hi All I am trying to remove the line having specific pattern from a file by using sed command I have the file named ODS_REP_SRCE_File.txt with content as: ... (1 Reply)
Discussion started by: Shilpi Gupta
1 Replies

2. Shell Programming and Scripting

sed text extraction between 2 patterns using variables

Hi everyone! I'm writting a function in .bashrc to extract some text from a file. The file looks like this: " random text Begin CG step 1 random text Begin CG step 2 ... Begin CG step 100 random text" For a given number, let's say 70, I want all the text between "Begin CG... (4 Replies)
Discussion started by: radudownload
4 Replies

3. UNIX for Dummies Questions & Answers

awk/sed match and extraction

Hi, I have a file like this- aa 12 23 34 aa 21 34 56 aa 78 45 56 I want to print out only the lines after the last aa. How do I do this? I tried using grep -A and sed -n, but both didnt work as I wanted to. Could someone help me out please.. (3 Replies)
Discussion started by: jamie_123
3 Replies

4. Shell Programming and Scripting

awk line extraction question

Hi, I am trying to extract the first and last lines for each unique item in column 2 of a large text file and then concatenate all extracted lines together in a new text file. So ... I want to go from this format: NEW 0088-BPM 1.042700e+04 877168.19 9718360.00 1496.00 NEW 0088-BPM... (5 Replies)
Discussion started by: jazthedribbler
5 Replies

5. Shell Programming and Scripting

Obscure sed extraction syntax

Hi, Could anyone clearly explain me the below sed construct in detail to get to know what it actually does? sed 's/\(* *\)//4' echo 'test;10;20' | sed 's/*;\(*\)/\1/' (1 Reply)
Discussion started by: royalibrahim
1 Replies

6. Shell Programming and Scripting

Extraction of text using sed or awk command

Hi All, I need to extract 543 from the command below : # pvscan PV /dev/sdb1 VG vg0 lvm2 Total: 1 543.88 GB] / in use: 1 / in no VG: 0 I have the following command which does the job, but I think this could be achieved in a more simple way using sed or awk. Any help is... (7 Replies)
Discussion started by: nua7
7 Replies

7. Shell Programming and Scripting

extraction of samba shares with sed

Hi there, My samba configuration file looks like that : ... ... path = /home/samba/profiles/ ... path = /home/samba/shares/family valid users = family path = /home/samba/shares/admins valid users = admins path = /home/samba/shares/publicI want to extract the list of standard... (3 Replies)
Discussion started by: chebarbudo
3 Replies

8. Shell Programming and Scripting

extraction of last but one char in a line

I need to extract the character before the last "|" in the following lines, which are 'N' and 'U'. The last "|" shouldn't be extracted. Also the no.s of "|" may vary in a line, but I need only the character before the last one. ... (1 Reply)
Discussion started by: hidnana
1 Replies

9. Shell Programming and Scripting

Sed/awk gods, I need your Help! Fancy log extraction

Hi! I'm trying to find a way to extract a certain amount of lines from a log file. This would allow me to "follow" a web user through our log files. Here is a sample fake log file to explain what i want to accomplish : BEGIN REQUEST sessionID=123456 boatload of lines for thread-1 detailing... (8 Replies)
Discussion started by: gnagus
8 Replies
Login or Register to Ask a Question