Printing value with no obvious field seperator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing value with no obvious field seperator
# 1  
Old 03-13-2008
Printing value with no obvious field seperator

Hi all,

Can anybody think of a way to do this? I have a file with content like the following:

F_TOP_PARAM_VALUE[0]F_TOP_SOURCE[UD1]F_TOP_DEL_NOTIFICATION[N]F_DEST_ADDR[76849]F_TOP_DEL_TYPE

What I want to do is print out the value in the square brackets after F_TOP_SOURCE. So in this case I'd like to print UD1 for this line. The file contains multiple lines such as the above and the number of parameters listed in the file can change which means that F_TOP_SOURCE[???] won't always be the second paramter list - so I could have a file with two lines like:

F_TOP_PARAM_VALUE[0]F_TOP_SOURCE[UD1]F_TOP_DEL_NOTIFICATION[N]F_DEST_ADDR[76849]F_TOP_DEL_TYPE
F_TOP_ORIG[6547]F_TOP_PARAM_VALUE[0]F_TOP_SOURCE[PS1]F_TOP_DEL_NOTIFICATION[N]F_DEST_ADDR[76849]F_TOP_DEL_TYPE

Any ideas? The only thing I can be sure about is that "F_TOP_SOURCE" will always precede the value in the bracket that I want, and an "F" will always follow the bracket I want.

Nightmare!

Any help would be much appreciated. I think it's also worth noting that I didn't design the format of the file! Smilie

Thanks in advance
# 2  
Old 03-13-2008
Try this with sed:

Code:
sed 's/.*F_TOP_PARAM_VALUE\[\(.*\)\[.*/\1/' file

Regards
# 3  
Old 03-13-2008
A Perl version:

Code:
cat temp.txt | perl -ne 'print "$_\n" if $_ =~ s/^.*F_TOP_SOURCE\[([^\]]+)].*$/$1/;'

This will print out the top source value for each line if there is one. If there isn't, it won't print anything for that line.

Also, this doesn't take into account multiple F_TOP_SOURCE sections in one line, so it will need to be modified if that can occur.

ShawnMilo
# 4  
Old 03-13-2008
GNUawk
Code:
awk 'BEGIN{ RS="F_TOP_SOURCE[[]|[]]"}
RT ~ /F_TOP_SOURCE/{getline;print}
' "file"

output:
Code:
# ./test.sh
UD1
UD1
PS1

# 5  
Old 03-14-2008
Thanks all, I used the perl solution which works really well.

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Field seperator with awk

Hi, input data format: echo ' <APPLICATION="APPLSG" SUB_APPLICATION="DLY" JOBNAME="DPL_BN_RE_CCMS_SA" CMDLINE="run_job.ksh %%PARAM1 %%PARAM2" TASKTYPE="Command" />' expected format: "APPLSG", "DLY", "DPL_BN_RE_CCMS_SA", "run_job.ksh %%PARAM1 %%PARAM2" my command: echo ' ... (2 Replies)
Discussion started by: JSKOBS
2 Replies

2. Shell Programming and Scripting

Printing uniq first field with the the highest second field

Hi All, I am searching for a script which will produce an output file with the uniq first field with the second field having highest value among all the duplicates.. The output file will produce only the uniqs which are duplicate 3 times.. Input file X 9 B 5 A 1 Z 9 T 4 C 9 A 4... (13 Replies)
Discussion started by: ailnilanjan
13 Replies

3. Shell Programming and Scripting

printing the max of each field

hi guys im very new to scripting and i have a problem. i need to use awk in my script and the script needs to print the max for each of the columns in a file. for example: numbers.txt 10 15 20 30 40 58 25 30 15 10 38 10 38 8 9 ./max numbers.txt 58 25 38 30 40 i have no clue on how to... (4 Replies)
Discussion started by: youness
4 Replies

4. Shell Programming and Scripting

field seperator question (awk script)

Is there a way I could use different a different field seperator for different parts of the body? kinda like {FS = ":"} FILENAME == "products"{ price = $3 if(numprods < $1-100) numprods = $1-100 } {FS = "/"}{} FILENAME == "associates"{ associateid... (5 Replies)
Discussion started by: angermanaged
5 Replies

5. Shell Programming and Scripting

Add a field seperator in a file.

"355"|""|"NJ"|"A0A 1W0"|"V"|""|""|""|"N" I've the above sample data seperated with pipe delimeter and in the file I want to replace a space with "|" to the 4th field so the result would be like below. So it would change from 9 fields to 10 fields. "355"|""|"NJ"|"A0A"|"1W0"|"V"|""|""|""|"N" ... (3 Replies)
Discussion started by: rudoraj
3 Replies

6. Shell Programming and Scripting

Printing the invert of the last field of awk

in csh set x = "/home/usr/dir1/file1" if i do: echo $x | awk -F\/ '{print $NF}' will result to: "file1" how do i invert the output to: "/home/usr/dir1" :confused: (2 Replies)
Discussion started by: jehrome_rando
2 Replies

7. Shell Programming and Scripting

How to change field seperator

Hi Please help me out with this problem: I want to have a script that would change the nth field seperator in a line into something else. like a,d,4,2,97,8,9 into a,d,4,2,97/8/9 Thanks (2 Replies)
Discussion started by: onthetopo
2 Replies

8. Shell Programming and Scripting

regexp to print after a field seperator

Hi, How do i Print anything after a ':' Ex : file1: 1235131(rs32553) I want to print out "1235131(rs32553)" how do i do it. I know we can do this using awk but looking for the right syntax. Any help appreciated. Thanks, Ram (7 Replies)
Discussion started by: ramky79
7 Replies

9. UNIX for Advanced & Expert Users

find columns with whitespace as field seperator?

Hai I am using bash-2.03$ bash --version GNU bash, version 2.03.0(1)-release (sparc-sun-solaris) I am not able to use gawk command its showing command not found , why ? Eg: awk 'NR==1' fix.txt | gawk 'BEGIN { FIELDWIDTHS = "3 2" } { printf($1"|"$2); }'... (8 Replies)
Discussion started by: tkbharani
8 Replies

10. Shell Programming and Scripting

Awk Field Seperator Help

I wrote a script on HPUX 11.11 to turn a Decimal subnet mask (255.255.254.0) to hex 0xfffffe00 (subset of a bigger script). It works great on the HPUX systems but on the freebsd box the awk is not seperating the fields properly. I tried to google for a solution and seaching these forums i am just... (3 Replies)
Discussion started by: insania
3 Replies
Login or Register to Ask a Question