Search for a word in file and output to another

Hello Experts!

I am trying to perform a common task, searching data from within a file and placing what is found into another.However, I have not been able to figure the �How to� with this situation.

I need to search through all lines within this text and pull the first positional attributes and their formats

For example, I have this line in the file:

LOGS MEAS FROM=(TABLE=INFO,FORMAT=A25,DESCRIPTION=Point from which logs are measured,ABBREV=LMF,STORAGE FORMAT=F10.3,MIRROR=YES)

I need to pick from this line the following and output to another file:

LOGS MEAS FROM     FORMAT=A25     FORMAT=F10.3

Because this is a very large file, I am also asking for your help to make this an efficient command.

Many Thanks for Your Help!
Lee

Please, no need to make all caps(increase font size)/bold!
Its like yelling/shouting at people.

This said, my motivation stoped at:

awk '{print $1" "$2" "$3" "$9}' OFS=", "  sample.txt

Outputs as follow:

LOGS MEAS FROM=(TABLE=INFO,FORMAT=A25,DESCRIPTION=Point FORMAT=F10.3,MIRROR=YES)

Hope this gets you started, though.

Have a nice day!
PS: I'm quite sure i've set the OFS wrong somehow.

Sorry about that. That was my second post ever on this site and I didn't know there was a font change.

Thanks for your Code input. I'm gonna try it out now.

Hi,
with gnu awk:

awk -F"[, ]" -vVAR="LOGS MEAS FROM" 'VAR { printf VAR;for(i=2;i<=NF;i++){ if ($i ~ /FORMAT=/) printf "\t"$i } print ""}'

Regards.

1 Like

Cheap, simple awk, create file a.awk that contains..

/^LOGS MEAS FROM.*FORMAT.*STORAGE FORMAT/  {
#               print
                split($0,a,"=")
                part1=a[1]
                split($0,a,",") 
                part2=a[2]
                split(a[5],b," ")
                part3=b[2]
                print part1,part2,part3
                }

suppose input file is a.txt, then...

[josephgr@freezer4 josephgr] awk -fa.awk a.txt
LOGS MEAS FROM FORMAT=A25 FORMAT=F10.3

or to capture output do..

awk -fa.awk a.txt > output.file
1 Like

THANKS disedorgue and blackragous!!

Both your suggestions worked. I tested each of them in my function call and they all worked good.

This site is great! Again, Thank you for helping a newbie. I'm really beginning to enjoy the scripting experience!