LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk, print special character, how? (https://www.linuxquestions.org/questions/linux-newbie-8/awk-print-special-character-how-941880/)

johnpaulodonnell 04-26-2012 10:54 AM

awk, print special character, how?
 
Hello,

Trying to figure out how to append the wildcard character * to each entry of the first column of a data file. I've tried various combinations of quotes and backslashes, but it just doesn't want to work for me.

Code:

awk '{print $1"*"}' somefile
Any input would be appreciated.

ntubski 04-26-2012 10:59 AM

Quote:

Originally Posted by johnpaulodonnell (Post 4663635)
but it just doesn't want to work for me.

"Doesn't work" meaning you lose the rest of columns? Try
Code:

awk '{$1=$1"*"; print}' somefile

johnpaulodonnell 04-26-2012 11:08 AM

Thanks for that. I just wanted to extract the first column but to tag * onto each entry from that column.


Code:

awk '{$1=$1"*"; print $1}' somefile
did the trick.

Appreciate your help.

Tinkster 04-26-2012 11:31 AM

How bizarre.... is that a MacOS thing? The original version of
yours works just fine here.
Code:

awk -F: '{print $1"*"}' /etc/passwd
root*
bin*
daemon*
adm*
lp*
sync*
shutdown*
halt*
mail*
news*
uucp*
operator*
games*
ftp*
smmsp*
mysql*
rpc*
sshd*
...

In what way was your original failing?


Cheers,
Tink

johnpaulodonnell 04-26-2012 12:30 PM

I thought that was where the problem lay as I was getting a
Code:

set: No match
error upon executing this piece of code within a tcsh script (on a MAC yes)...

Turns out I had to add
Code:

set nonomatch
prior to the command and all is now well. I don't know what this does, but it works so I'm happy.


Code:

set list = `ls -l $dir/D*bp03 | awk '{print $9}' | awk 'BEGIN {FS="."} {print $2}' | awk 'BEGIN {ORS=" "} {print  "D*"$0".Z"}'`
echo $list
set: No match.


Code:

set nonomatch
set list = `ls -l $dir/D*bp03 | awk '{print $9}' | awk 'BEGIN {FS="."} {print $2}' | awk 'BEGIN {ORS=" "} {print  "D*"$0".Z"}'`
echo $list
D*NAMA.Z D*KGMA.Z D*TUND.Z D*PNDA.Z D*UVZA.Z D*KIG.Z D*MBAR.Z D*KMBO.Z

which is what I wanted.

David the H. 04-26-2012 12:35 PM

"$1" prints only the first column, and he apparently wants all of the columns in the output.

Edit: just read the above more closely. :doh:

Let this be a lesson. Don't just say "it doesn't work" and leave everyone to guess what you mean. Explain exactly why it isn't working, and how it should work. Preferably with actual examples of input and output.


Anyway, if the number of columns is fixed and manageable, you could also simply include them in the print statement.

Code:

awk '{ print $1"*",$2,$3,$4,$5 }' somefile

ntubski 04-26-2012 12:48 PM

Code:

set list = `ls -l $dir/D*bp03 | awk '{print $9}' | awk 'BEGIN {FS="."} {print $2}' | awk 'BEGIN {ORS=" "} {print  "D*"$0".Z"}'`
Nice example of Don't parse ls and XyProblem. I'm not sure what that line is computing, but I'm sure there is a simpler way.


All times are GMT -5. The time now is 09:46 PM.