Searching for + sign in a string using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for + sign in a string using awk
# 1  
Old 01-10-2008
Searching for + sign in a string using awk

Hi All,

My query is:
I have string say xyz+ how to determine that whether it ends with a + sign or not using awk command.
# 2  
Old 01-10-2008
Quote:
Originally Posted by satyajit2512
Hi All,

My query is:
I have string say xyz+ how to determine that whether it ends with a + sign or not using awk command.
Code:
# awk '/\+$/' file

# 3  
Old 01-11-2008
sed is easy to do such thing

Try this one:

Code:
sed -n '/+$/p' a

# 4  
Old 01-11-2008
quick query for ghostdog

Bro,

wudnt the code that u mentioned search for "xyz+" sting only if it is at the end of a line ? pls correct me if I am wrong

what if the string which has to be searched happens to be in the middle of a line. like
asd sdvdh dfh "xyz+" shfks dfbv
# 5  
Old 01-11-2008
Quote:
Originally Posted by GRUBBERR
Bro,

wudnt the code that u mentioned search for "xyz+" sting only if it is at the end of a line ? pls correct me if I am wrong
yes only at the end of a string, as OP requested
Quote:
what if the string which has to be searched happens to be in the middle of a line. like
asd sdvdh dfh "xyz+" shfks dfbv
a little modification then, but the regexp remains
Code:
...
 for ( i=1;i<=NF;i++) {
  if ( $i ~ /+$/ ) {
    # do something
  }
 }
..

# 6  
Old 01-11-2008
Shorter one:

Code:
> echo "asd sasa+ sd+lvdh dfh xyz+ shfks dfbv ddfd+\c"|awk 'match($0,/\+$/)' RS=" "       
sasa+
xyz+
ddfd+

Or:

Code:
> echo 'asd sasa+ sd+lvdh dfh xyz+ shfks dfbv ddfd+'|awk 'gsub(/\n/," ");match($0,/\+$/)' RS=" " 
sasa+
xyz+
ddfd+

Or:
Code:
> echo "asd sasa+ sd+lvdh dfh xyz+ shfks dfbv ddfd+"|awk 'gsub(/\n/," ");index($0,"+") == (length) ' RS=" "                       
sasa+
xyz+
ddfd+

Or:
Code:
> echo "asd sasa+ sd+lvdh dfh xyz+ shfks dfbv ddfd+\c"|awk 'index($0,"+") == (length) ' RS=" "               
sasa+
xyz+
ddfd+


Last edited by Klashxx; 01-11-2008 at 05:43 AM.. Reason: Another option added.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: greater than sign is working upside down

Hi, I noticed a weird behaviour with awk. input: A|B|1-100|blabla_35_40_blabla;blabla_53_60_blabla;blabla_90_110_blabla Objective: For each string separated by ';' in $4, if the first and second numbers are included in the interval in $3, then print "TRUE". Otherwise print "FALSE". In... (3 Replies)
Discussion started by: beca123456
3 Replies

2. Shell Programming and Scripting

Percentage sign causing awk problems

looks like awk gets confused when there's a % next to a number. command im running: awk -F" " '/phxnaz001b/ && /vol/ && NF { if (($NF >= 80) && ($NF < 83)) { print ; print ; w++ } else if ($NF >= 83) { print ; c++ } } END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }' /tmp/test.log ... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. UNIX for Dummies Questions & Answers

Insert sign every n character (awk)

Hi, For example, I would like to insert a pipe every 4 characters for each second field (including after the last block). input (coma separated): line1,AAAABBBBCCCCDDDDEEEE line2,FFFFGGGGHHHHIIIIJJJJ output: line1,AAAA|BBBB|CCCC|DDDD|EEEE| line2,FFFF|GGGG|HHHH|IIII|JJJJ| my... (2 Replies)
Discussion started by: beca123456
2 Replies

4. Shell Programming and Scripting

how to get data from hex file using SED or AWK based on pattern sign

I have a binary (hex) file I need to parse to get some data which are encoded this way: .* b4 . . . 01 12 .* af .* 83 L1 x1 x2 xL 84 L2 y1 y2 yL By another words there is a stream of hexadecimal bytes (in my example separated by space for better readability). I need to get value stored in... (3 Replies)
Discussion started by: sameucho
3 Replies

5. Shell Programming and Scripting

Searching a String

Hi everyone ! suppose i'm searching for a specific string in a file so it is very easy, i use the following command grep 'keyword' file_name but how to search a word which is repeated maximum number of times in a file, for example in the following text i have to search a word which is... (12 Replies)
Discussion started by: ourned
12 Replies

6. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

7. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

Sign on/Sign off logging script

I'd like to make a script that I can execute every time I sign on to my linux box that keeps track of the time and allows to me to add a remark to a file. So basically once I log in, I run the script, and it outputs the date and time to a text file (log.txt). But that isn't my problem. I need... (1 Reply)
Discussion started by: Glider
1 Replies

10. Shell Programming and Scripting

one more query for searching string.......

Dear friends, I have one more query, incase a file contains multiple tags of same name, then how to get the required string between the tags, in which the string begins with "O/M" i.e., file1.txt contains following text(please note that all the following tags are in single line)... (6 Replies)
Discussion started by: swamymns
6 Replies
Login or Register to Ask a Question