awk to use variable instead of stdin or file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to use variable instead of stdin or file
# 1  
Old 04-27-2016
awk to use variable instead of stdin or file

on linux systems, i can do something like this:
Code:
$ JIOO=hello.one.two
$ 
$ awk -F"." '{print $2}' <<< "${JIOO}"

however, on older systems or other unix systems that dont have the fancy stuff this does not work.

i contemplated using "-v var="${JIOO}" but i dont think that works.

any ideas?

Last edited by RudiC; 04-27-2016 at 03:18 PM.. Reason: Added icode tags.
# 2  
Old 04-27-2016
Hello SkySmart,

Not sure about your complete requirement, you could try following code and let me know if this helps you. In case you have any other requirement please do let us know the Input_file and sample output.
Code:
echo ${JIOO} | awk -F"." '{print $2}'
OR
echo $JIOO | awk '{split($0, A,".");print A[2]}'

Thanks,
R. Singh

Last edited by RavinderSingh13; 04-27-2016 at 03:34 PM.. Reason: Added one more solution on same now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 04-27-2016
Quote:
Originally Posted by SkySmart
.
.
.
i contemplated using "-v var="${JIOO}" but i dont think that works.
.
.
.
Why shouldn't that work (with some measures taken)?
Code:
awk -F"." -v var="${JIOO}" 'BEGIN {$0=var; print $2}'
one

This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-27-2016
There is a portable shell-built-in alternative for that kind of token splitting. It has the side-effect of overwriting your $1 $2 ... argument variables, though. If your shell at least supports functions, putting that in a function should prevent that.

Code:
VAR="a.b.c.d.e"
OLDIFS="$IFS" ; IFS="."
set -- $VAR # Note $VAR must be UNquoted here, we depend on the shell's splitting
IFS="$OLDIFS"

# $1 will be a, $2 b, $3 c, etc.

This may be preferable over using awk repeatedly, since awk is an external programming language, not a shell built-in. Using awk repeatedly to split single lines is like making 9 phone calls to say 9 words, extremely inefficient.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-27-2016
Quote:
Originally Posted by SkySmart
on linux systems, i can do something like this:
...
however, on older systems or other unix systems that dont have the fancy stuff this does not work.
This is not related to the fact you are running on Linux or Unix. The point here is the shell you run. If you run dash on Linux, this command will fail and if you run ksh93 on Unix, it will work as you expect.
# 6  
Old 04-27-2016
Quote:
Originally Posted by Corona688
There is a portable shell-built-in alternative for that kind of token splitting. It has the side-effect of overwriting your $1 $2 ... argument variables, though. If your shell at least supports functions, putting that in a function should prevent that.

Code:
VAR="a.b.c.d.e"
OLDIFS="$IFS" ; IFS="."
set -- $VAR # Note $VAR must be UNquoted here, we depend on the shell's splitting
IFS="$OLDIFS"

# $1 will be a, $2 b, $3 c, etc.

This may be preferable over using awk repeatedly, since awk is an external programming language, not a shell built-in. Using awk repeatedly to split single lines is like making 9 phone calls to say 9 words, extremely inefficient.
Thank you so much. i needed this! i hope this works on older systems!!

---------- Post updated at 03:28 PM ---------- Previous update was at 03:27 PM ----------

Quote:
Originally Posted by RavinderSingh13
Hello SkySmart,

Not sure about your complete requirement, you could try following code and let me know if this helps you. In case you have any other requirement please do let us know the Input_file and sample output.
Code:
echo ${JIOO} | awk -F"." '{print $2}'
OR
echo $JIOO | awk '{split($0, A,".");print A[2]}'

Thanks,
R. Singh
thank you
i wanted to avoid using echo. i should have stated that in my original post. sorry. Smilie
# 7  
Old 04-27-2016
Quote:
Originally Posted by SkySmart
Thank you so much. i needed this! i hope this works on older systems!!
It's an extremely old syntax, I believe. It works in almost any shell I can throw at it -- including zsh, which isn't even Bourne anymore..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Accept data from file or stdin in script

I have a script that looks like this:sed -f myfile.sed $1 > $1.out called myscript and would like to change it so the parameter isn't necessary: ls *.idx | myscript | xargs some_command What do I need to add so it can run either way? TIA ---------- Post updated at 09:41 AM ----------... (1 Reply)
Discussion started by: wbport
1 Replies

2. Shell Programming and Scripting

FILENAME of awk with stdin

Hello: How can I print out the FILENAME with awk when the input is from STDIN? zcat SRR1554449.fq.gz | awk ' (length($2) >= 300) {print FILENAME}'this will print out - - - ....as when awk reads from the standard input, and FILENAME is set to "-". But I am expecting sth like:... (5 Replies)
Discussion started by: yifangt
5 Replies

3. Shell Programming and Scripting

Awk variable in a separate file

Hi all, I have a requirement to put all the varibles used in an awk command in a separate file. This is because i have arround 100 variables used in an awk command. So i want to put all the variables used for the awk command in a separate file. Please help me on this. Thanks in adv. (6 Replies)
Discussion started by: gani_85
6 Replies

4. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

5. UNIX for Dummies Questions & Answers

Use awk in a variable, not in a file...?

I know that sed can do this. Can also awk? I mean instead of awk '{ print $NF }' < filename to use awk '{ print $NF }' < $variable (1 Reply)
Discussion started by: hakermania
1 Replies

6. Shell Programming and Scripting

sed replace string in file with stdin

Hi Im trying to do the following: grep -H Date: out/* | sed 's/':'/ /' | awk '$4 ~ /^/ {print $1}' | while read VARIABLE; do awk '{print $1,$3,$2}' $VARIABLE | sed (take stdin and replace a string in $VARIABLE) done What this is basically doing is finding all files with Date: in... (11 Replies)
Discussion started by: duonut
11 Replies

7. Programming

Read redirected file from stdin in C (a.out < file)

Hello everybody, Having a file with the following content: 192.168.0.254->192.168.0.1 192.168.0.2->192.168.0.34 192.168.0.56->192.168.0.77 I need to code a program in C to read it from stdin redirection (i.e. root@box~# ./a.out < file), my question is, how can i do that? I've tried with... (2 Replies)
Discussion started by: semash!
2 Replies

8. AIX

STDIN Devie File Name and Location

Hi all, I want to know the device filename of STDIN in AIX. On other platforms(Linux and solaris), the device file for stdin is available at /dev/ directory as "/dev/stdin". But i didn't find any filename for STDIN at /dev/ directory in AIX. Please let me know the name and location of device... (3 Replies)
Discussion started by: erra_krishna
3 Replies

9. HP-UX

STDIN Devie File Name and Location

Hi all, I want to know the device filename of STDIN in HPUX. As the same is available on other platforms at /dev/ directory as "/dev/stdin", i can't find any filename for STDIN at /dev/ in HPUX. Please let me know the name and location of device file of STDIN on HPUX. Thanks regards,... (0 Replies)
Discussion started by: erra_krishna
0 Replies

10. Programming

Changing stdin from file redirection to console input

Hi I am doing file redirection at console for use by my binary. %console%> bin &lt inputfile After reading in the entire file, I want my program to continue taking input from the console. So essentially I want to redirect stdin back to console. But I cant figure out how to do it. I am... (4 Replies)
Discussion started by: nauman
4 Replies
Login or Register to Ask a Question