Search for a word in file and output to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a word in file and output to another
# 1  
Old 04-07-2015
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:
Code:
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:
Code:
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
# 2  
Old 04-07-2015
Please, no need to make all caps(increase font size)/bold!
Its like yelling/shouting at people.

This said, my motivation stoped at:
Code:
awk '{print $1" "$2" "$3" "$9}' OFS=", "  sample.txt

Outputs as follow:
Code:
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.
# 3  
Old 04-07-2015
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.
# 4  
Old 04-07-2015
Hi,
with gnu awk:
Code:
awk -F"[, ]" -vVAR="LOGS MEAS FROM" 'VAR { printf VAR;for(i=2;i<=NF;i++){ if ($i ~ /FORMAT=/) printf "\t"$i } print ""}'

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 04-07-2015
Cheap, simple awk, create file a.awk that contains..
Code:
/^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...
Code:
[josephgr@freezer4 josephgr] awk -fa.awk a.txt
LOGS MEAS FROM FORMAT=A25 FORMAT=F10.3

or to capture output do..
Code:
awk -fa.awk a.txt > output.file

This User Gave Thanks to blackrageous For This Post:
# 6  
Old 04-07-2015
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. Shell Programming and Scripting

How to search a word in a file?

How to search a particular word in a file. in my file contains lacks of rows. (2 Replies)
Discussion started by: pmreddy
2 Replies

3. Shell Programming and Scripting

word search in output

How to get the fisrt word of the output of a shell script . below command will display the list of baselines in my view . cmd : cleartool lsstream -fmt "%NXp\n" -view $VIEW_NAME Output : baseline:MHC_BUILDTREE1.0.1 baseline:JEPG_DUIF_CI baseline:MOR_BuildTree_BLD_I.0.1 I need to... (2 Replies)
Discussion started by: saku
2 Replies

4. Shell Programming and Scripting

search a word and copy the file

Hi need help with a script or command My requirement is - I need to do a "ls -ltr tcserver*.syslog" files in /tmp, direct the output to a file ls -ltr tcserv*.syslog | grep "Jan 31" | awk '{printf "\n" $9}' > jandat.logs -Then open each file in above list and search for string/word,... (4 Replies)
Discussion started by: karghum
4 Replies

5. Shell Programming and Scripting

How to search for a word in a particular column of a file

How to search for a word like "computer" in a column (eg: 4th field) of a '***' delimited file and add a column at the end of the record which denotes 'Y' if present and 'N' if not. After this, we need to again check for words like 'Dell' but not 'DellXPS' in 5th field and again add another column... (5 Replies)
Discussion started by: Jassz
5 Replies

6. Shell Programming and Scripting

Search for last instance of word in a file

Hi I'm trying to search for the last instance of the word 'cache' in a HTML file that I have downloaded from YouTube. I'm using the following syntax, but an error is thrown when I try it. grep -f "cache" Also I wish to append the above grep command to the below so that the search for cache... (3 Replies)
Discussion started by: colmbell
3 Replies

7. UNIX for Dummies Questions & Answers

Search a specific word from any one of the file.

how do i Search a specific word from any one of the file.? (1 Reply)
Discussion started by: ritusubash
1 Replies

8. Shell Programming and Scripting

To search a file for a specific word in a file using shell script

Hi All, I have a sql output file has below. I want to get the values 200000040 and 1055.49 .Can anyone help me to write a shell script to get this. ACCOUNT_NO ------------------------------------------------------------ BILL_NO ... (8 Replies)
Discussion started by: girish.raos
8 Replies

9. UNIX for Dummies Questions & Answers

search for a word and it's occurance at the output

hey to everybody this is my first post at this forum I need a very immediate answer for this question. If you can, I will be delightfull I have a file called example.txt and I want to seek for the for hello and learn the number of the occurance of hello (2 Replies)
Discussion started by: legendofanatoli
2 Replies

10. Shell Programming and Scripting

search a word from file

Hello, I have a file which contains some SQL statements. I need to take out the table name from the file. Table name will always end with "_t". can anyone help me in getting that? for e.g. --- SQL_STMT do_sql_insert: cmd="insert into account_t ( poid_DB, poid_ID0, poid_TYPE, poid_REV,... (9 Replies)
Discussion started by: vishy
9 Replies
Login or Register to Ask a Question