0

Edited

This is the content of example.dat file:

AAA*00*          *00*          *66*1234567890     *ZZ*ASD-LKJ        *170630*1933*U*00401*100000023*0*P*\~SS*IN*2035735845*JWN-DSD*20660606*0606*1023*X*DF4WE43R4RA~BIG*20660606*2771417*19990919*88146417~GE*3*1023~IEA*1*100000023~

The data I needed are inside a segment. A segment is separated by ~ and the data I needed is called an element. An element is separated by a *.

These are the pattern to find those strings:

Segments         Element number          Needed Data
  AAA            6th, 8th & 13th         1234567890, ASD-LKJ & 100000023
  SS             6th                     1023
  BIG            4th                     88146417
  GE             1st                     3

Is there a command that outputs these data?

100000023    1023    3    1234567890    ASD-LKJ    88146417

Note: In the real world, the file contains really long set of strings and many segments but the pattern of segments and the place where the elements positioned stays the same.

  • is the input single line or multiple line? what is the logic to extract strings? not able to see a pattern... and as AAA is only once in input string, how do you expect multiple output? – Sundeep Jul 4 '17 at 4:53
  • It's single line. Actually it's a file with a really long set of strings. I just shorten it for just an example. I'll update the patterns for everyone to understand further. – WashichawbachaW Jul 4 '17 at 7:24
  • 1
    it is still not clear on what basis you need to extract data from each of these segments... and why does last segment you show has ~ in it – Sundeep Jul 4 '17 at 7:43
  • Please edit the whole question again and give sample input and output. – hschou Jul 4 '17 at 9:23
  • Please see my last update. I think it's understandable enough. You can edit my question if you want. – WashichawbachaW Jul 5 '17 at 0:20
1

This will output your example data in your example output format

$ cat script.awk
BEGIN { FS="*"; }
$1=="AAA" { printf "%s\n%s\n%s\n", $7, $9, $14 }
$1=="SS"  { print $7 }
$1=="BIG" { print $5 }
$1=="GE"  { print $2 }
$ awk -f script.awk example.dat
1234567890     
ASD-LKJ        
100000023
1023
88146417
3
  • When I type "cat script.awk" it returns "cat: script.awk: No such file or directory". How to do that? Sorry I'm just new to Linux/Unix. Do you have a simpler command like 1 line just like grep? – WashichawbachaW Jul 5 '17 at 2:57
  • How to write that in shell? Do I do this: 1st to type: [current_directory]$ cat script.awk 2nd: BEGIN { FS="*"; } [current_directory]$ 1=="AAA" { printf "%s\n%s\n%s\n", $7, $9, $14 } [current_directory]$ 1=="SS" { print $7 } [current_directory]$ 1=="BIG" { print $5 } [current_directory]$ 1=="GE" { print $2 } [current_directory]$ awk -f script.awk example.dat But's it's not working for me. I'm doing it wrong. Kindly explain it further. I'm just new to Linux/Unix. – WashichawbachaW Jul 6 '17 at 8:47
  • Use a text editor such as vi or nano to create script.awk – fpmurphy Jul 7 '17 at 2:47
  • I tried vi script.awk, stored BEGIN { FS="*"; } up to $1=="GE" { print $2 }, save it chmod 755 script.awk and then finally run the command awk -f script.awk example.dat but I only get these three output 1234567890 ASD-LKJ 100000023 – WashichawbachaW Jul 7 '17 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.