How to print the extended regular expression ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print the extended regular expression ?
# 1  
Old 09-01-2011
How to print the extended regular expression ?

Hello,
How to print the field separator in awk? please see the following code:
cat a.txt
Code:
a 1s  2s   3s     4s
b  2s    4s

Code:
[river@localhost Desktop]$ awk  'BEGIN{FS==" "} {print $2 $3 }' te 
1s2s
2s4s

I want to get the following output :
Code:
1s  2s
2s    4s

How to realize this ?

Code:
[river@localhost Desktop]$ cat te
a 1s,,2s   3s     4s
b  2s    4s
[river@localhost Desktop]$ awk  'BEGIN{ FS = "[,| ]" } {print $2 $3 }' te 
1s
2s
[river@localhost Desktop]$

I want to get the following result:
Code:
1s,,2s
2s    4s

How to realize it ?

Last edited by 915086731; 09-01-2011 at 04:10 AM..
# 2  
Old 09-01-2011
There is OFS - output field separator in awk. Either change it in BEGIN block, or add FS manually:
Code:
{print $1 FS $2}

# 3  
Old 09-01-2011
However ,
Code:
{print $1 FS $2}

I get the following :
Code:
[river@localhost Desktop]$ awk  ' {print $2 FS  $3 }' te 
1s,,2s 3s                               
2s 4s                                     !!only one space
[river@localhost Desktop]$

In origin file, the content between "2s" and "4s" is 4 spaces, but it only print 1 space.
and I need to specify FS to be "," or " ".
# 4  
Old 09-01-2011
Adjacent characters will be threaded as multiple field separators.

When FS=",", a,,b will be interpreted as a null b.


It would be easy with Perl:

Code:
% cat infile 
a 1s,,2s   3s     4s
b  2s    4s
% perl -nle'
  print +(split /\s(?!\s)/)[1,2]
  ' infile
1s,,2s  3s    
2s   4s

# 5  
Old 09-01-2011
Oh, sorry, I decide not to use perl, thanks.
# 6  
Old 09-01-2011
Why not Perl?

Which operating system and awk version are you using?
# 7  
Old 09-01-2011
I am using fedora 15 , bash environment.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print byte position of extended ascii character

Hello, I am on AIX. When I encounter extended ascii characters and special characters on a file I need to print.. Byte position, actual character and line number. Is there a simple command that can give me the above result ? Thanks in advance (38 Replies)
Discussion started by: rosebud123
38 Replies

2. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Would like to print 3 lines after a regular expression is found in the logfile

I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code: grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done The above code things works fine,but sometimes gives erroneous... (3 Replies)
Discussion started by: joachimshaun
3 Replies

6. Shell Programming and Scripting

Regular Expression.

can someone let me know what this means in english. \(abcd\) \ is an escape key right? Thanks Also im getting confused with something like it does this mean any single character? and this would be 2 characters ? Just let me know if im on the right track. (5 Replies)
Discussion started by: syco__
5 Replies

7. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

8. Shell Programming and Scripting

Regular expression in grep -E | awk print

Hi All, I have file.txt with contents like this: random text To: recipient@email.co.uk <HTML>S7randomtext more random text random text To: recip@smtpemail.com <HTML>E5randomtext more random text random text I need the output to look like this: 1,,,1,S7 1,,,1,E5 My code so... (9 Replies)
Discussion started by: terry2009
9 Replies

9. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question