Simple awk question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple awk question
# 8  
Old 10-22-2012
Thanks a lot Jotne
# 9  
Old 10-22-2012
Awk example works on all lines, but not the echo example

Code:
var='Happy_world_foo123
Happy me 2
Nobody like my foo6564'

echo gives
Code:
echo "${var#*foo}"
123
Happy me 2
Nobody like my foo6564

or
Code:
echo ${var#*foo}
123 Happy me 2 Nobody like my foo6564

# 10  
Old 10-22-2012
Here are two ways to get what I believe was requested:
Code:
echo using sed:
sed -n '/foo/s/[^0-9]*\([0-9]*\).*/\1/p' in
echo using ask:
awk '/foo/{if(match($0, /[0-9]+/)) printf("%s\n", substr($0, RSTART, RLENGTH))}' in

assuming that the input lines you want to process are in a file named in. Note that I don't see anything asking for the digits to be printed only if they follow "foo".
# 11  
Old 10-22-2012
Code:
$ echo Happy_world_foo123 | sed '/foo/s/[^0-9]//g'
123

# 12  
Old 10-22-2012
Quote:
Originally Posted by Jotne
[CODE]
If you have other characters after number or more than one foo in same line, this does not work.
How about printing $NF and checking for numeric fields and using sub to remove characters..?
# 13  
Old 10-22-2012
Code:
$ echo Happy_world_foo123 | sed '/foo/s/[^0-9]//g'

Does not work on this:
Code:
var='Happy_world_foo123
Happy me 2
Nobody like my foo6564'

Code:
echo "$var" | sed '/foo/s/[^0-9]//g'
123
Happy me 2
6564

# 14  
Old 10-22-2012
Code:
$ echo "$var" | nawk '{gsub("[A-Za-z_]","",$0)}1'
123
2
6564

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple awk question

Here is an awk line I have in a bigger script that checks to see if nimsh process is running and does couple other things based on the output and runs on all servers. ps -ef|grep -i nimsh|awk '{print $9}' and I am expecting output to be "/usr/sbin/nimsh" I find that on some servers... (4 Replies)
Discussion started by: kvosu
4 Replies

2. Shell Programming and Scripting

awk help - Simple question

I have what a think is a simple question but I'm just a beginner in scripting. I'm my unix command line I run a date command that returns the following: Wed Apr 3 10:39:30 EDT 2013 How do I awk out the "10" only in awk? Or is awk the way to do it or is there a better way? (7 Replies)
Discussion started by: scj2012
7 Replies

3. UNIX for Dummies Questions & Answers

awk simple question

Can anyone tell me please what the "+" is doing in this awk command? find / -user smith -type f -ls | awk '{ sum += $7 } END {print sum }' Thanks, George (2 Replies)
Discussion started by: george_vandelet
2 Replies

4. UNIX for Dummies Questions & Answers

Simple AWK question

Hi, let's assume i have an output below: orgauser 23826 :E:Validity senerse 2096 senerse 2111 senerse 21585 senerse 21596 root 12653 -bash root 17262 root 17278 Some lines have not any string in their third column. I don't want to see those lines. i just want to see the lines... (3 Replies)
Discussion started by: oduth
3 Replies

5. Shell Programming and Scripting

simple awk question: split field with :

Hi, Probably a very weak question.. but I have tried all I know.. BPC0001:ANNUL_49542 0.0108 -0.0226 -0.0236 0.0042 0.0033 -0.0545 0.0376 0.0097 -0.0093 -0.032 Control BPC0002:ANNUL_49606 0.0190 -0.0142 -0.0060 -0.0217 -0.0027 ... (3 Replies)
Discussion started by: genehunter
3 Replies

6. Shell Programming and Scripting

simple awk/sed/tr question

I have a file CREATE TABLE DDD_EXT --- 1000 ( val u1 val u1 ); CREATE TABLE dsdasd_EXT --- 1323 ( val u1 val u1 ); CREATE TABLE AAAAAA_EXT --- 1222 ( val u1 val u1 ); CREATE TABLE E_EXT --- 11 ( val u1 val u1 (2 Replies)
Discussion started by: jville
2 Replies

7. UNIX for Dummies Questions & Answers

simple awk question

Hi , I have a simple question in awk, i have long string which i am getting for a grep command. the output contains 50 fields. I need to display like first 5 fileds in a line and rest of all fields in the next line. { for(i=5;i<NF;++i) s= $i; print $1,$2,$3,$4,$5,"\n",$s} Is the above... (1 Reply)
Discussion started by: senthilkumar_ak
1 Replies

8. UNIX for Dummies Questions & Answers

simple awk question

Hello, I'm trying to use awk to print lines that match a regular expression. I am using awk to print a record only if it contains N/A. awk '/N/A/ {print $1}' When executed the script returns "awk syntax error near line 1". If I use /N//A/ it prints all records containing a "/", not... (2 Replies)
Discussion started by: orahi001
2 Replies

9. Shell Programming and Scripting

Simple awk If Satement question.

Trying to write an if statement, which calls2 or 3 functions from within it. code: {if (($34 != "") && (NR != 1) && ($1 != "F")) less_than(34, 0, "S1002a") is_number(34,"S1002a") } But this is only treating the first function call, as part of the if. and always executes is_number.... (1 Reply)
Discussion started by: natdeamer
1 Replies

10. Shell Programming and Scripting

Simple awk script question

Hi, I'm a total beginner at awk and hope someone can advise what I have done wrong in the following script: I have a file which (to simplify things) may be something like this Fred Smith and Sue Brown Joe Jones and Jane Watts Sally Green and Jim O? Connor Freda O? Reiley and Pat O?... (2 Replies)
Discussion started by: Bab00shka
2 Replies
Login or Register to Ask a Question