Should I use sed/ grep/awk for wrap file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Should I use sed/ grep/awk for wrap file?
# 1  
Old 05-03-2006
Error 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 02:17 PM..
# 2  
Old 05-03-2006
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\}\*'
# 3  
Old 05-03-2006
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.
# 4  
Old 05-03-2006
You can also use:
Code:
echo $LINE|awk 'FS="\*" {for (i = 1; i <= NF; ++i){ if ($i ~ /[0-9]{11}/) print $i }}'

Regards
# 5  
Old 05-03-2006
Hi Klashxx, could you break up the awk command that you posted? Thx
# 6  
Old 05-04-2006
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
# 7  
Old 05-04-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. UNIX for Dummies Questions & Answers

line wrap in sed

I tried line wrap in sed by sed -l 20 ' n' and as far as i understand each long line would be split into lenght of 20 characters in each. but thats not happening.wats going wrong:confused: (5 Replies)
Discussion started by: mithun1!
5 Replies

3. Shell Programming and Scripting

Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV. The strings could be on different line numbers, depending on size of log. Example Log File: File = foo.bat Date = 11/11/11 User = Foo Bar Size = 1024 ... CSV should look like: "foo.bat","11/11/11","Foo Bar","1024" (7 Replies)
Discussion started by: chipperuga
7 Replies

4. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

5. Shell Programming and Scripting

Word wrap with sed

Hi, I got some timetable in a file but it is all mixed up like this 01:00 hgrtwhrt #104:00 tyergethr05:00 tqqrthd qrth #107:00 qhtrhqerth10:00 qerthrthqr qtrqthr qthrrt11:00 thqrthqrthrr rthgreth #212:00 trhrthrth14:00 wrthwrtwrqrthwrthwr #2116:00 trqhthtr: rthrthr17:00 rtwhtrhwrth rthwrt... (6 Replies)
Discussion started by: stinkefisch
6 Replies

6. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies

7. Shell Programming and Scripting

Big data file - sed/grep/awk?

Morning guys. Another day another question. :rolleyes: I am knocking up a script to pull some data from a file. The problem is the file is very big (up to 1 gig in size), so this solution: for results in `grep "^\ ... works, but takes ages (we're talking minutes) to run. The data is held... (8 Replies)
Discussion started by: dlam
8 Replies

8. HP-UX

word wrap issue with grep

in my HP-Unix environment I continue to have issues seeing the whole file path when I do a grep Example: >ps -ef |grep test > /testpath/is/here/ should see: >ps -ef |grep test > /testpath/is/here/not/here/test Is there a setting to turn word wrap on/off? It works fine in our AIX... (4 Replies)
Discussion started by: bowtiextreme
4 Replies

9. UNIX for Advanced & Expert Users

WEB Server Log File Analysis using awk/sed/grep

I'm trying to find a way to show large page sizes (page size in K) from multiple web server log files. Essentially I want to show only rows from a file where a specific column is larger than some value. Has anyone ever done this type of log analysis? If so, a snippet of code would be very... (2 Replies)
Discussion started by: mike_cataldo@ad
2 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question