Parse a String for a Specific Word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse a String for a Specific Word
# 1  
Old 02-15-2011
Parse a String for a Specific Word

Hello,

I'm almost there with scripting, and I've looked at a few examples that could help me out here. But I'm still at a lost where to start. I'm looking to parse each line in the log file below and save the output like below.

Log File
HTML Code:
AABBCGCAT022|242|3
AABBCGCAT023|243|4
PLATODCAT024|244|5
PLATODCAT025|245|6
SOCRATCAT026|246|7
SOCRATCAT027|247|8
Desired Output
HTML Code:
GCAT022
      AABBC        3
GCAT023
     AABBC        4
File name to save as /tmp/aabb.csv

DCAT024
      PLATO       5
DCAT025
      PLATO       6
File name to save as /tmp/plat.csv

TCAT026
      SOCRA      7
TCAT027
      SOCRA      8
File name to save as /tmp/socra.csv
# 2  
Old 02-15-2011
Try this:
Code:
awk -F\| '{print substr($1,6,7) RS "\t" substr($1,1,5) "\t" $3 > tolower(substr($1,1,4))".csv"}' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 02-15-2011
Thanks Franklin! It works as expect! I just have to figure out expressions then I might be able do what you did there, LOL!

---------- Post updated at 10:55 AM ---------- Previous update was at 06:47 AM ----------

Franklin,

My apologies, I noticed the following error, and it's not outputing a portion of the log cause I fail to mention I'm running Solaris.

awk: too many output files 10
record number 157
# 4  
Old 02-15-2011
There are too many files open, try this:
Code:
awk -F\| '{
  fn=tolower(substr($1,1,4)) ".csv"
  print substr($1,6,7) RS "\t" substr($1,1,5) "\t" $3 > fn
  close(fn)
}' file

Use nawk or /usr/xpg4/bin/awk on Solaris.
This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 02-15-2011
nawk works, however for each file create it only has the first line from the Log File in them.

NAWK OUTPUT
HTML Code:
      
the saved file /tmp/aabb.csv only has one line in it  like below.
GCAT022
      AABBC        3
the saved /tmp/plat.csv file only has one line in it like below.
DCAT024
      PLATO       5

the saved file /tmp/socra.csvonly has one line in it like below.

TCAT026
      SOCRA      7
Please see above for Desired Output.

Thanks for your help again Franklin!
# 6  
Old 02-15-2011
try this:
Code:
print substr($1,6,7) RS "\t" substr($1,1,5) "\t" $3 >> fn

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 02-15-2011
Yes! Works perfectly now! I did read about the switch to append to a current file, guess I won't forget it now. Thanks for all your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get string before specific word in UNIX

Hi All, I'm writing unix shell script and I have these files. I need to get name before _DETL.tmp. ABC_AAA_DETL.tmp ABC_BBB_DETL.tmp ABC_CCC_DETL.tmp PQR_DETL.tmp DEF_DETL.tmp JKL_DETL.tmp YUI_DETL.tmp TG_NM_DDD_DETL.tmp TG_NM_EEE_DETL.tmp GHJ_DETL.tmp RTY_DETL.tmp output will... (3 Replies)
Discussion started by: ace_friends22
3 Replies

2. 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

3. Shell Programming and Scripting

Shell scripting: frequency of specific word in a string and statistics

Hello friends, I need a BIG help from UNIX collective intelligence: I have a CSV file like this: VALUE,TIMESTAMP,TEXT 1,Sun May 05 16:13:05 +0000 2013,"RT @gracecheree: Praying God sends me a really great man one day. Gotta trust in his timing. 0,Sun May 05 16:13:05 +0000 2013,@sendi__... (19 Replies)
Discussion started by: kraterions
19 Replies

4. Shell Programming and Scripting

Help with calculating frequency of specific word in a string

Input file: #read_1 AWEAWQQRZZZQWQQWZ #read_2 ZZAQWRQTWQQQWADSADZZZ #read_3 POGZZZZZZADWRR . . Desired output file: #read_1 3 #read_1 1 #read_2 2 #read_2 3 #read_3 6 . . (3 Replies)
Discussion started by: perl_beginner
3 Replies

5. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

6. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

7. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies

10. Shell Programming and Scripting

Finding a word at specific location in a string

Hi All , I have different strings (SQL queries infact) of different lengths such as: 1. "SELECT XYZ FROM ABC WHERE ABC.DEF='123' " 2. "DELETE FROM ABC WHERE ABC.DEF='567'" 3. "SELECT * FROM ABC" I need to find out the word coming after the... (1 Reply)
Discussion started by: swapnil.nawale
1 Replies
Login or Register to Ask a Question