The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
usage of module Text::Wrap; trek Shell Programming and Scripting 3 06-04-2008 11:08 PM
word wrap issue with grep bowtiextreme HP-UX 4 04-30-2008 04:39 PM
SCO acquisition wrap-up iBot UNIX and Linux RSS News 0 02-19-2008 01:30 PM
Wrap Interactive Script meskue Shell Programming and Scripting 0 06-23-2006 03:21 PM
word wrap in vi dangral UNIX for Dummies Questions & Answers 3 10-30-2002 10:45 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-03-2006
Registered User
 

Join Date: May 2006
Posts: 4
Stumble this Post!
Exclamation Should I use sed/ grep/awk for wrap file?

Hi,
This is my first time post a new thread. I have been trying to work on this for the past 2 days and could not find any good solution.
I have 1 long long line ( EDI wrapped file) like below:
NEW*SR*04411763447*279*278*Q~*ZR*AAV*SR*04511763460*SQ*21B37F04~HL*305*304*Q~K~SN1*1*1*SR*0551176346 1* and so on...all in 1 long line.

I need to extract certain pattern =grep 'SR\*[0-9]\{11\}\*' ( example SR*04411763447*) and remove others and put it to output file like:
SR*04411763447*
SR*04511763460*
SR*05511763461*
.
. etc.

I tried use grep and sed..but have no luck working on wrap file.
Please help.

Last edited by vanda_25; 05-03-2006 at 10:17 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-03-2006
Registered User
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 130
Stumble this Post!
I've been working with X12 documents for the last year and a half and finally wrote a C program to print out the segments one per line. However, a nice quick-and-dirty trick is to convert the segment terminator (usually, but not always, a tilde) to a newline.

# cat file | tr '~' '\n' | grep 'SR\*[0-9]\{11\}\*'
Reply With Quote
  #3 (permalink)  
Old 05-03-2006
Registered User
 

Join Date: May 2006
Posts: 4
Stumble this Post!
Thank you so much. I wish I post this 2 days ago.
I added cut cmd..a bit tuning and it should be ok.
cat file.txt |tr '~' '\n' | grep 'SR\*[0-9]\{11\}\*'| cut -c 75-85 >final.txt

Thanks a bunch.
Reply With Quote
  #4 (permalink)  
Old 05-03-2006
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 371
Stumble this Post!
You can also use:
Code:
echo $LINE|awk 'FS="\*" {for (i = 1; i <= NF; ++i){ if ($i ~ /[0-9]{11}/) print $i }}'
Regards
Reply With Quote
  #5 (permalink)  
Old 05-03-2006
Registered User
 

Join Date: May 2006
Posts: 4
Stumble this Post!
Hi Klashxx, could you break up the awk command that you posted? Thx
Reply With Quote
  #6 (permalink)  
Old 05-03-2006
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 371
Stumble this Post!
Code:
$ cat fields.dat 
NEW*SR*04411763447*279*278*Q~*ZR*AAV*SR*04511763460*SQ*21B37F04~HL*305*304*
     R*01234567891*279*278*Q~*ZR*AAV*SR*04511763460*SQ*21B37F04~HL*305*304*
NEW*SR*09876543210*279*278*Q~*ZR*AAV*SR*04511763460*SQ*21B37F04~HL*305*304*
$ cat fields.awk 
awk '{

FS="\*"

{
for (i = 1; i <= NF; ++i)
   { 
   if ($i ~ /[0-9]{11}/) 
   print $i 
   }
}

}' 
$ fields.awk <fields.dat
04411763447
04511763460
01234567891
04511763460
09876543210
04511763460
Cheers
Reply With Quote
  #7 (permalink)  
Old 05-04-2006
Registered User
 

Join Date: May 2006
Posts: 4
Stumble this Post!
Hi Klashxx,
I got an error that said:
awk: Input line ISA*00* *00 cannot be longer than 3,000 bytes.
I guess my input file is too big to handle for awk? My files mostly more than 1200 Kilo bytes.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0