Awk, find and print


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Awk, find and print
# 1  
Old 01-14-2018
Awk, find and print

Ubuntu, Bash 4.3.48

Hi,

I have this input file with many columns separated with ":"

Code:
ARC=121:ERF=12244:IDE=2334:ADA=34 ....
ERF=124:ARC=123:IDE=2344:ADA=54 ....
ERF=16254:IDE=2434:ADA=78:ARC=134 ....

and I want this:
Code:
ARC=121:IDE=2334
ARC=123:IDE=2344
ARC=134:IDE=2434

I need to use awk because with this code " grep -oh "\w*ARC=\w*" " or similar it isn't flexible.. many thanks!

echo manolis
# 2  
Old 01-14-2018
Hello echo manolis,

Could you please try following and let me know if this helps you.
Code:
awk '{match($0,/ARC=[0-9]+/);val1=substr($0,RSTART,RLENGTH);match($0,/IDE=[0-9]+/);print val1":"substr($0,RSTART,RLENGTH)}'  Input_file

Output will be as follows.
Code:
ARC=121:IDE=2334
ARC=123:IDE=2344
ARC=134:IDE=2434

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-14-2018
ok, many thanks! It is perfect! One more question...

Input
Code:
ARC=3.215465:ERF=12244:IDE=122/43:ADA=34 ....
ERF=1235:ARC=5.244:IDE=14/4:ADA=54 ....
ERF=568:IDE=2/43:ADA=78:ARC=1.6254 ....

Output
Code:
ARC=3.2:IDE=122/43
ARC=5.2:IDE=14/4
ARC=1.6:IDE=2/43

Of course I have to use the same code, just to add for the val1= (ARC) the simbo %.1f ... somewhere ... and for the val2= (IDE) the tags [0-9]\/[0-9] ???

Could you help me again please!

Starting code:

Code:
awk '{match($0,/ARC=[0-9]+/);val1=substr($0,RSTART,RLENGTH);match($0,/IDE=[0-9]+/);val2=substr($0,RSTART,RLENGTH);print val1":"val2}' Input_file


Last edited by echo manolis; 01-15-2018 at 03:22 AM..
# 4  
Old 01-14-2018
Quote:
Originally Posted by echo manolis
ok, many thanks! It is perfect! One more question...

Input
Code:
ARC=3.215465:ERF=12244:IDE=122/43:ADA=34 ....
ERF=1235:ARC=5.244:IDE=14/4:ADA=54 ....
ERF=568:IDE=2/43:ADA=78:ARC=1.6254 ....

Output
Code:
ARC=3.2:IDE=122/43
ARC=5.2:IDE=14/4
ARC=1.6:IDE=2/43

Of course I have to use the same code, just to add for the val1= (ARC) the simbo %.1f ... somewhere ... and for the val2= (IDE) the tags [0-9]\/[0-9] ???

Could you help me again please!

Starting code:

Code:
awk '{match($0,/GT=[0-9]+/);val1=substr($0,RSTART,RLENGTH);match($0,/AF=[0-9]+/);val2=substr($0,RSTART,RLENGTH);print "val1":"val2}' Input_file

Are you just searching on-line for solutions to your problems? Or, have you read the awk man page on your system and tried using it to construct code that meets your requirements?

Why would we use the above Starting code which is looking for strings that contain GT= and AF= followed by one or more decimal digits and has mismatched double-quotes in the print statement instead of starting with the code Ravinder suggested in post #2 that will give you output close to what you want instead of reporting a syntax error and printing nothing?

Working from Ravinder's code, extending the match() extended regular expressions to accept periods or slashes in addition to decimal digits and using sprintf() to round the numbers after ARC= to 1 decimal place, one might want to try something more like:
Code:
awk '
{	match($0, /ARC=[0-9.]+/)
	val1 = sprintf("ARC=%.1f", substr($0, RSTART + 4, RLENGTH - 4))
	match($0, "IDE=[0-9/]+")
	print val1":"substr($0, RSTART, RLENGTH)
}'  Input_file


Last edited by Don Cragun; 01-14-2018 at 09:53 PM.. Reason: Fix typo: s/zero/one/
# 5  
Old 01-15-2018
Hi Don Cragun,

I made some confusion between examples and codes. I corrected my last post. However thank you very much!

Best,
echo manolis
# 6  
Old 01-15-2018
Hello echo manolis,

Could you please try following and let me know if this helps you.
Code:
awk '{match($0,/ARC=[0-9]+\.[0-9]+|ARC=[0-9]+/);val1=substr($0,RSTART,RLENGTH);match($0,/IDE=[0-9]+\/[0-9]+|IDE=[0-9]+/);print val1 ":" substr($0,RSTART,RLENGTH)}'   Input_file

Output will be as follows.
Code:
ARC=3.215465:IDE=122/43
ARC=5.244:IDE=14/4
ARC=1.6254:IDE=2/43

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

awk to find text and print value

In the attached file I am using awk to skip the header row, search for specific text, and print particular values that are next to it all from the same field. awk FNR > 1 '$79 == "Bases in target regions"":" "Number of amplicons"': "Target bases with no strand bias"':" {print $79}' >... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

awk find and print next string

Can I do this in one awk session. Solution I have is poor. I want to return the number after PID. echo "Start: 12345 is used by PID:11111 username" | awk -F: '{print $3}' | awk '{print $1}' (6 Replies)
Discussion started by: u20sr
6 Replies

4. Shell Programming and Scripting

Find and print specific date with awk

hi all I would like to help me find the problem with this script to find and print to the screen a specific date of a log file that I have on my server, the date it is received as the first argument in the script $ 1 Here I show you a few lines that made ​​the idea of ​​my log file: ****... (4 Replies)
Discussion started by: gilmore666
4 Replies

5. Shell Programming and Scripting

Awk find in columns with "if then" statement and print results

I have a file1.txt file1.txt F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true" F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true" U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true"... (4 Replies)
Discussion started by: charles33
4 Replies

6. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

7. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies

8. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

9. UNIX for Dummies Questions & Answers

AWK...find a string ,if so print it

Hi, I need to find a string, if it finds then I need to print it , otherwise it has to goto next line.... input is====> uid = shashi, india uid ,uid= asia uid= none, uid=india. none ========== output shold be uid = shashi, india uid , uid= asia uid= none, uid=india. none ... (1 Reply)
Discussion started by: hegdeshashi
1 Replies

10. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question