Get string from file after nth symbol


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get string from file after nth symbol
# 1  
Old 10-20-2008
Get string from file after nth symbol

I've got a file, where I want to grab two fields from it. The file isn't fixed format, so all I know is that the two fields are after a certain number of delimiters within the record.

File e.g. as follows:-

"1"^"HEADER"^
"5"^"12345678"^"Smith"^"Dave"^"Mr"^"Research & Development"^"Sheffield"^"ABCDEFGH"^
"9"^"TRAILER"^

if position 2 = '5', then after the 1st ^ write the value between the ^'s to the file, then delimit by a comma (or anything), then after the 7th ^ write the value to the file, so I end up with:-

"12345678","ABCDEFGH"
# 2  
Old 10-20-2008
Below script will print the 2nd and 8th column if the first column is "5" assuming '^' as the delimeter

awk -F"^" '{if($1=="\"5\"") print $2 "," $8}' filename
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

3. Shell Programming and Scripting

Get nth occurence of string from a file

I have file in which the data looks like this, 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ 03,test1,41203016,,/ 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ ... (16 Replies)
Discussion started by: r@v!7*7@
16 Replies

4. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

5. UNIX for Dummies Questions & Answers

grep line for string up to symbol

Hi, I would like to extract a pattern from a line. The first two characters will always be the same in this pattern, but the proceeding numbers will not be, and the pattern will always be 6 characters long. I would like to get the entire pattern up to a certain symbol, in this case, a period. ... (3 Replies)
Discussion started by: goodbenito
3 Replies

6. Shell Programming and Scripting

Check if string starts with $ symbol

How do I check that a string $AA22CC3 starts with the "$" symbol ? I have tried : checksum='$AAB3E45' echo $checksum case $checksum in $* ) echo success ; esac Thanks ... (4 Replies)
Discussion started by: cillmor
4 Replies

7. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

8. Shell Programming and Scripting

Replacing a string in nth line

Hello All, How to replace a string in nth line of a file using sed or awk. For Ex: test.txt Line 1 : TEST1 TEST2 TEST3 Line 2 : TEST1 TEST2 TEST3 TEST4 Line 3 : TEST1 TEST2 TEST3 TEST5 Line 4 : TEST1 TEST2 TEST3 TEST6 Line 5 : TEST1 TEST2 TEST3 TEST7 i want to go to 4th line of a... (1 Reply)
Discussion started by: maxmave
1 Replies

9. Shell Programming and Scripting

replace nth instance of string

Hi all, I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using... (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. UNIX for Dummies Questions & Answers

How do I get the nth character from a string?

How to I get the nth character from a string in shell script. For instance, I have a string London. I want to get, say the first character (L) from the string. How do I do this in unix shell? Thankx (4 Replies)
Discussion started by: toughman
4 Replies
Login or Register to Ask a Question