Script to match strings that sometimes are splitted in 2 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to match strings that sometimes are splitted in 2 lines
# 8  
Old 08-25-2013
OK, you are using GNU awk. With gawk you need to use another command line option: --non-decimal-data

Code:
gawk --re-interval --non-decimal-data -v ...

probably easiest to use:
Code:
gawk --posix -v ...

Or use a gawk-only function like you suggested...

Last edited by Scrutinizer; 08-26-2013 at 03:10 AM..
# 9  
Old 08-25-2013
Hello Scrutinizer,

Thank you, I'll try it the decimal convertion.

Now I'm trying is to put 2 variables in the same awk script to get other pattern. It seems to work, but is printing the matched pattern of variable 2 in next line.

Code:
awk -v pat1="ff 44( [^ ]{2}){3,4} 32 14 56( [^ ]{2}){5}" -v pat2="pattern2" '
  {  b=p $0 ;c=p $0 }
  match(b,pat1) {  print substr(b,RSTART,RLENGTH) sub(pat1,x)  }
  match(c,pat2) {  print substr(c,RSTART,RLENGTH) sub(pat2,z)  }
  {    p=$0 FS  }' file

How to print value of variables b and c in the same line?

Thanks again.
# 10  
Old 08-25-2013
You could try
Code:
  match(b,pat1) {s=substr(b,RSTART,RLENGTH); sub(pat1,x) }
  match(c,pat2) {s=s (s!=""?OFS:"") substr(c,RSTART,RLENGTH); sub(pat2,z)  }
  s!="" {print s; s=""}


Last edited by Scrutinizer; 08-25-2013 at 09:08 AM..
# 11  
Old 08-25-2013
Hello Scrutinizer,

Thanks, it works that way.

The pat2 could be in more than line, so to could use your code, intead of have 32 elements in a line I changed it to 64 with xxd command, and without spaces.

So, your first code continue working, but I don't know why is not printing all patterns even they are in the file, I got this output:
Code:
$ awk -v pat="ff44.{6,18}321456.{5}" '
  {    b=p $0  }
  match(b,pat) {    print substr(b,RSTART,RLENGTH); sub(pat,x)
  }
  {    p=$0 FS  }' file2.txt
ff4400000232145601757
ff4400000332145601766
ff4400000432145601789

But the correct output should be (the patterns that have the sequence 01 and 05 are not being printed):
Code:
ff4400000132145600238
ff4400000232145601757
ff4400000332145601766
ff4400000432145601789
ff4400000532145601788

The input file is:
Code:
97444444444c52975f529744979744303730359797333003504947480fffffff
44000001321456002383952f50494748000fffff0015000a4800015a00021700
024200016000013300013600013700017e00016900006a000079000094000193
00012200002100010900010a00012600016b00016c00006d0000020001040001
0500010600011000010800012b00002c00002d00002e00005500005600072a00
002f0000300000970000ff34ff44000002321456017570698f50494748001fff
ff0015000c4800015a000a1700011800014200016000013600013700017e0001
6900006a00007900009400019300012200002100000900010a00011000010800
012b00002c00002d00002e00005500005600072a00002f0000300000970000ff
34ff44000003321456017664454f50494748003fffff0015000c4800015a0017
1700011800014200016000013600017e00016900017900009400019300012200
002100000900010a00011000010800012b00002c00002d00002e000055000056
00072a00002f0000300000970000ff34ff44000004321456017890003f504947
48004fffff0015000c4800015a000a1700011800014200016000013600017e00
016900017900009400019300012200002100000900010a00011000010800012b
00002c00002d00002e00005500005600072a00002f0000300000970000ff34ff
44000005321456017887761f50494748005fffff0015000a4800015a000b4200
016000013300013600013700016600016500017700016900006a000079000094
00019300012200002100010900010a00012600011000010800012b00002c0000
2d00002e00005500005600072a00002f0000300000970000ff3403810f010200
00000d5049526905ffffff008970010c0000000d5049526905ffffff0101860f
010c0000000d50495269559fffff00840e01020102010001ffffff0201020185

Thanks in advance for the help.
# 12  
Old 08-25-2013
Hi, it should be p=$0 instead of p=$0 FS


--
Note that this is different from your original specification, in the sense that lengthwise, patterns could now occur more than once on the same line. If this is a possibility, the code would need to be adjusted..
# 13  
Old 08-25-2013
Hello Scrutinizer again.

It works how you said when the file has 128 characters per line, I'm not sure why doesn't
print when has 256 characters in each line.

Well, may you please show me an example how to separate the content of each variable by space or comma, let say:
- for variable b print characters 1 to 2, 3 to 4, 5 to 11, 12 to 27, 28 to 43.
and between each range a space.
- for variable c print characters 1 to 5, 6 to 12

If it is possible to assign to those sub ranges to a new variable could be better, because I would like to do this for presentation issue and in order to be able in the future, for example to print one or more of that ranges in decimal or with another format.

Thanks for all help so far.

Regards
# 14  
Old 08-26-2013
You would need to use substr(s,pos,1) for every character. Example:
Code:
$ awk 'BEGIN{s="abcdefg"; print substr(s,4,1), substr(s,2,1)}'
d b

I guess one could create a function:
Code:
$ awk 'function char(s,c){return substr(s,c,1)} BEGIN{s="abcdefg"; print char(s,4), char(s,2)}'
d b

Some awks have a special case where, by using an empty string as field separator, a string can be separated into fields that contain a single character:
Code:
$ awk 'BEGIN{s="abcdefg"; split(s,F,x); print F[4], F[2]}'
d b

The latter however is not portable..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. Shell Programming and Scripting

Delimited records splitted into different lines

Hi I am using delimited sequence file. Delimter we are using is pipe .But for some of the records for one of the column the values are getting split into different lines as shown below "113"|"0155"|"2016-04-27 07:59:04"|"1930"|"TEST@TEST"|"2016-04-27 11:04:04.357000000"|"BO"|"Hard... (13 Replies)
Discussion started by: ginrkf
13 Replies

3. Shell Programming and Scripting

Script to match lines in screen

I'd like to ask people who knows bash scripting to write me a script which would open a specific screen and match lines. Here is algorithm I'm thinking about. Find SCREENS named name1, name2.... and nameX. Open them one by one and type 'STATS' Match last lines of the screen before command... (3 Replies)
Discussion started by: GhostMan
3 Replies

4. Shell Programming and Scripting

Returning two lines if they both match strings

Hi I have a problem where I have a large amount of files that I need to scan and return a line and its following line, but only when the following line begins with a string. String one - line one must begin with 'Bill' String two - line two must begin with 'Jones'. If these two... (7 Replies)
Discussion started by: majormajormajor
7 Replies

5. Shell Programming and Scripting

Print only lines where fields concatenated match strings

Hello everyone, Maybe somebody could help me with an awk script. I have this input (field separator is comma ","): 547894982,M|N|J,U|Q|P,98,101,0,1,1 234900027,M|N|J,U|Q|P,98,101,0,1,1 234900023,M|N|J,U|Q|P,98,54,3,1,1 234900028,M|H|J,S|Q|P,98,101,0,1,1 234900030,M|N|J,U|F|P,98,101,0,1,1... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

6. Shell Programming and Scripting

Script to multi-transfer splitted files via scp

Hey :3 I am moving some stuff between different servers. I do it like this: scp -r -P 22 -i ~/new.ppk /var/www/bigfile.tar.gz user@123.123.123.123:/var/www/bigfile.tar.gz Lets say, this file is 50 GiB. I would like to know, if its possible to split the file in different parts,... (2 Replies)
Discussion started by: Keenora
2 Replies

7. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

8. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

9. Shell Programming and Scripting

shell script: grep multiple lines after pattern match

I have sql file containing lot of queries on different database table. I have to filter specific table queries. Let say i need all queries of test1,test2,test3 along with four lines above it and sql queries can be multi lines or in single line. Input file contains. set INSERT_ID=1; set... (1 Reply)
Discussion started by: mirfan
1 Replies

10. Shell Programming and Scripting

Perl script to match a pattern and print lines

Hi I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9. I appreciate any help with code Thanks Ammu (6 Replies)
Discussion started by: ammu
6 Replies
Login or Register to Ask a Question