Visit Our UNIX and Linux User Community

Concatenate a string and number and compare that with another string in awk script

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Concatenate a string and number and compare that with another string in awk script
# 1  
Old 08-27-2019
Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script

if ( $0 ~ /SVC IN:/ )
    svc_in=substr( $0,23 , 3);
    if (msg_start == 1 && msg_end == 0)
else if ( $0 ~ /^SVC OUT:/ )
    svc_out=substr( $0, 9, 3);
      if (msg_start == 1 && msg_end == 0)
 else if ( $0 ~ /^MSGEND/ )
 if ( $svc_in$svc_out != "ABCABC" )
print .....
print ..

Moderator's Comments:
Mod Comment Please use code tags

The issue here is it works fine when both in and out values are strings and gets an error when one of them is a number.
My input file has diff combinations, how can we convert it when we get a number in any one value?

Last edited by jim mcnamara; 08-27-2019 at 09:10 PM..
# 2  
Old 08-27-2019
svc_in svc_out != "ABCABC"

(do not use $'s in front of variables)

Last edited by rdrtx1; 02-18-2020 at 09:22 PM..
# 3  
Old 08-27-2019
Do we need to give space in between svc_in and svc_out or without any space?
# 4  
Old 08-27-2019
give space. otherwise it will think it's another variable.

Last edited by rdrtx1; 02-18-2020 at 08:53 PM..
This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 08-27-2019
The space character is the concatenation operator in awk

$ echo "333" | awk '{ $0="space in front " $0 "no space after"; print $0}'
space in front 333no space after

This User Gave Thanks to jim mcnamara For This Post:
# 6  
Old 08-28-2019
This worked, Thanks for your help !!
# 7  
Old 08-29-2019
Put a string concatenation in parenthesis to better visualize the order!
if ( (svc_in svc_out) != "ABCABC" )

Previous Thread | Next Thread
Test Your Knowledge in Computers #133
Difficulty: Easy
The first prototypes of Linux were publicly released in 1991.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare string length to a number

Hi, I want to compare strings length to a number but i am getting error. I want first name should be length of 8. Please help. #bin !/bin/bash clear echo -n "Enter name " read name IFS=_ ary=($name) for key in "${!ary}"; do echo "$key${ary}"; done ##First name should be equal to 8... (8 Replies)
Discussion started by: rajneesh4U
8 Replies

2. Shell Programming and Scripting

Pass column number as variable to awk and compare with a string.

Hi All, I have a file test.txt. Content of test.txt : 1 vinay se 2 kumar sse 4 kishore tl I am extracting the content of file with below command. awk '$2 ~ "vinay" {print $0}' test.txt Now instead of hardcoding $2 is there any way pass $2 as variable and compare with a... (7 Replies)
Discussion started by: Girish19
7 Replies

3. Shell Programming and Scripting

String compare using awk - what am I doing wrong?

Hi all, I was doing some string manipulation in my script and wanted to try using awk. However, I have been stuck with string compare. A simplified form of my conundrum is given below: The below prints expected result (prints "Completed because that is the second element"): $ echo... (5 Replies)
Discussion started by: faraway
5 Replies

4. Shell Programming and Scripting

awk - how to compare part of the string?

Need help for awk.. file will have comma separated numbers, I need check digits before 10 numbers eg ( 001)1234567890 Basically want to check country code of a mobile number. eg: abc,def,data, data,0011234567890, data,data Script should be checking country code with 001, I will pass... (10 Replies)
Discussion started by: vegasluxor
10 Replies

5. Shell Programming and Scripting

Awk, sed - concatenate lines starting with string

I have a file that looks like this: John Smith Marc Olsen Lynne Doe (3 Replies)
Discussion started by: locoroco
3 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Concatenate lines with unique string AND number

In Bash using AWK or sed I need to convert the following file: ... numitem_tab0 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p> subitem_tab0 =<p></p><p></p> ... numitem_tab6 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>... (2 Replies)
Discussion started by: pioavi
2 Replies

8. Shell Programming and Scripting

awk if statement to evaluate string and compare

I have the following simplified code that I am planning on putting into a larger shell script. I have been butchering it to try and make work amongst google searches and reading awk documentation. amixer sset Master toggle | awk '{ if ( /^ Front Left/ { print $7 } == // ) print "MUTED" }'I... (2 Replies)
Discussion started by: jelloir
2 Replies

9. Shell Programming and Scripting

[awk]compare a number in a string with a list

Hi, I have a program written in awk and I want to extend it to do another task. My program is a list of CVS log reports of a repository. For each file, I have some fields. One of the fields is the comment field. I want to know how I can check if a comment (which is a free text field)... (8 Replies)
Discussion started by: sandeepk1611
8 Replies

10. Shell Programming and Scripting

Concatenate String through Awk

I want to concatenate any particular field of the file with any String say SSB....but i am not able to do it... I hv tried the following code....but its saying there is error in parsing it.. awk 'BEGIN { FS = "," ; OFS = "," ; } { for ( i = 1 ; i < 5 ; i++ ) {a=i;b="SSB"; print $1,$a$b,$3 } }'... (3 Replies)
Discussion started by: monu_munish
3 Replies

Featured Tech Videos