LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2020, 07:50 AM   #1
carlolab
LQ Newbie
 
Registered: Apr 2020
Posts: 3

Rep: Reputation: Disabled
BASH - variable as variable


Hi,

I have a "fileX" test file with a content like this:

a b c d e f



I need to make a for cicle to print column 2 to 6


for col in {2..6}
do
cat fileX | awk '{ print $$col }'
done



The problem is to pass variable $col as variable in print function of awk.
The part '{ print $$col }' shold be read as '{ print $2 }' then '{ print $3 }' etc.etc.

Can someone help me?

Thank You

Carlo
 
Old 04-11-2020, 08:14 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,337
Blog Entries: 3

Rep: Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732
You could do the loop within AWK, or you could take advantage of the -v option to pass an environment variable into the AWK script.

Code:
#!/bin/sh

for col in {2..6}
do
        cat fileX | awk -v c=col '{ print $c }'
done

exit 0
That will print the different columns, from 2 through 6, on different lines.

If you want them all on the same line then put the loop inside AWK.

Code:
cat fileX \
| awk '{ for (col=2;col<=6;col++) { printf("%s%s",$col,OFS);} print "";}'
See "man awk" for either method. It is a short manual.
 
Old 04-11-2020, 08:26 AM   #3
carlolab
LQ Newbie
 
Registered: Apr 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
You could do the loop within AWK, or you could take advantage of the -v option to pass an environment variable into the AWK script.

Code:
#!/bin/sh

for col in {2..6}
do
        cat fileX | awk -v c=col '{ print $c }'
done

exit 0
That will print the different columns, from 2 through 6, on different lines.

If you want them all on the same line then put the loop inside AWK.

Code:
cat fileX \
| awk '{ for (col=2;col<=6;col++) { printf("%s%s",$col,OFS);} print "";}'
See "man awk" for either method. It is a short manual.

Thank you for answering.

I tried but it didn't work.

#!/bin/sh

for col in {2..6}
do
cat fileX | awk -v c=col '{ print $c }'
done

exit 0



It print:

a b c d e f
a b c d e f
a b c d e f
a b c d e f
a b c d e f



Carlo
 
Old 04-11-2020, 08:31 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,337
Blog Entries: 3

Rep: Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732
My mistake, I missed that {2..6} is a bashism. Change the first line, and add in the dollar sign I forgot while we're in there.

Code:
#!/bin/bash

for col in {2..6}
do
        cat fileX | awk -v c=$col '{ print $c }'
done

exit 0
and it will produce a different result. I typed #!/bin/sh by reflex.
 
Old 04-11-2020, 08:38 AM   #5
carlolab
LQ Newbie
 
Registered: Apr 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
My mistake, I missed that {2..6} is a bashism. Change the first line, and add in the dollar sign I forgot while we're in there.

Code:
#!/bin/bash

for col in {2..6}
do
        cat fileX | awk -v c=$col '{ print $c }'
done

exit 0
and it will produce a different result. I typed #!/bin/sh by reflex.

It works!

Thank You very much Turbocapitalist!


Carlo
 
Old 04-11-2020, 09:50 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,992

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
you can read the file as an array in bash:
Code:
read -r -a arr <fileX
printf "%s\n" "${arr[@]}"
(not tested)

from the other hand you do not need cat, you can simply use:
Code:
awk 'script' fileX
 
Old 04-13-2020, 10:05 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,812

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by carlolab View Post
Code:
for col in {2..6}
do
cat  fileX | awk '{ print $$col }'
done
The problem is to pass variable $col as variable in print function of awk.
The part '{ print $$col }' shold be read as '{ print $2 }' then '{ print $3 }' etc.etc.
My first thought was that you should use braces: "$$col" -> "${${col}}" or "${$col}" to avoid something like:
Code:
$ echo $$col
12345col
But I think your biggest problem would be substituting that into your awk command because of the single quotes and getting the different values of `col' that you need.

Perhaps building the awk command string up a bit at a time and issuing "eval awk $CMDSTR" would work.

Or doing the whole thing in awk (sorry... been too long since I wrote much awk code).
 
Old 04-13-2020, 10:57 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,875
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
@OP: The actual input-file contains only one line? If not, then please provide a more complete example.
 
Old 04-14-2020, 02:52 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Just expanding a bit on pan64's solution from #6. You can slice arrays in bash, too:
Code:
printf %s\\n "${arr[@]:1}"
 
  


Reply

Tags
bash script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
-bash: HISTSIZE: readonly variable -bash: HISTFILESIZE: readonly variable deathsfriend99 Linux - Newbie 4 12-08-2009 12:51 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
bash scripting need help getting a variable of a variable dovo Programming 6 10-29-2008 01:30 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration