nawk and space in the parameter

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nawk and space in the parameter
# 1  
Old 08-04-2011
nawk and space in the parameter


Could you please tell me how nawk command works when there is a asterisk <*> or space with asterisk <[space] *> or <*[space] > in the parameter.

I am just trying to read line by line and fetch fourth parameter separated by delimiter (|).
But if there is a * or <[space] *> or <*[space] > in the fourth parameter it gets all the files in that directory. How to address this issue..

PARAM4=`echo $LINE | nawk -F"|" '{print $4}'
echo $PARAM4

output for echo comes like that 123 file1.csv file2.txt

Last edited by radoulov; 08-04-2011 at 09:05 AM.. Reason: Code tags!
# 2  
Old 08-04-2011
It's not awk, it's the shell ...
You need to quote the parameters in order to avoid shell filed splitting and filename generation (globbing)!

PARAM4=`echo "$LINE" | nawk -F"|" '{print $4}'
echo "$PARAM4"

Or better:

  printf '%s\n' "$LINE" | 
    nawk -F"|" '{ print $4 }'

printf '%s\n' "$PARAM4"

This User Gave Thanks to radoulov For This Post:
# 3  
Old 08-04-2011
second option works good.. Thank you..
if i want to make some kind of string manipulation on PARAM4, how should i do.. like using sed or etc., when i use PARAM4 with sed, i getting stuck with same problem again.
# 4  
Old 08-04-2011
We'll need to see a sample of your input and an example of the desired/expected output.
This User Gave Thanks to radoulov For This Post:
# 5  
Old 08-04-2011

Here is the script <test.ksh> i use

LINE="nramak|BFS|121 * 789 * 85#2[123]|7539|"
PARAM4=$(printf '%s\n' "$LINE" | nawk -F"|" '{ print $3 }')
printf '%s\n' "$PARAM4"

PARAM5=`echo "$PARAM4" |  sed 's/\[/ /g'  | sed 's/\]/ /g' | sed 's/#/ /g'`

echo $PARAM5

when i ran it, i got below:
121 * 789 * 85#2[123]
121 test.ksh 789 test.ksh 85 2 123

seems like i have trouble in line#5 while using sed command on PARAM4.

Any idea how to get this done.
Thanks in advance!

Last edited by radoulov; 08-04-2011 at 12:11 PM.. Reason: Code tags.
# 6  
Old 08-04-2011
Try this:

line='nramak|BFS|121 * 789 * 85#2[123]|7539|'

  printf '%s\n' "$line" |
    cut -d\| -f3 |
          tr ']#[' '   '

printf '%s\n' "$param4"

This User Gave Thanks to radoulov For This Post:
# 7  
Old 08-04-2011
It works. Thank you very much
Which command gives better performance sed or tr
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

2. Shell Programming and Scripting

Pwd with space paths but on the parameter don't work

hi, i'm finding to solve on the parameter: for example: directory Value 1 root@value 1 > pwd /home/user/root/value 1 root@value 1 > pwd | sed 's/ /\\ /g' /home/user/root/value\ 1 root@value 1 > test="$(pwd | sed 's/ /\\ /g')" root@value 1 > echo "$test" /home/user/root/value\ 1 ... (4 Replies)
Discussion started by: gsflash80
4 Replies

3. Shell Programming and Scripting

Getting foreach to read a parameter with blank space

my program is designed to take the first parameters as extension, then the rest of the parameters as files to be searched for and, if found, modified by the extension. If not found, it prints an error. Everything is great until: ./ 'com' 'king cobra.dat' where $file splits up the two... (2 Replies)
Discussion started by: username652719
2 Replies

4. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

5. Shell Programming and Scripting

Dot operator and space, Parameter not set

Hi, i have this script if then echo "is empty" fi echo "done" The following is the result when i run the script from command without and with a dot and space operator $ is empty done $ . sh: VAR_1: Parameter not set. $ It's our standard to run... (5 Replies)
Discussion started by: ysrini
5 Replies

6. Shell Programming and Scripting

Pass parameter to nawk from shell script

I need to parse log files using nawk, but I'm not able to pass script input argument (date) to nawk, for example: ------------ #!/bin/ksh read date nawk -F, '{if($1==date) print $4" "$5}' ------------- Is there a way to pass an argument to nawk from shell script. Many thanks... (8 Replies)
Discussion started by: samer.odeh
8 Replies

7. Shell Programming and Scripting

Passing Parameter containing space in between to Shell Script

Hi, I have one shell script which use two parameter however one of its parameter have space in between. eg. 20110114 b c d here b c d is one parameter I used 'b c d' but its not giving correct result. Also i tried b\c\d but this one also didnt work. Any help would be... (5 Replies)
Discussion started by: diehard
5 Replies

8. Shell Programming and Scripting

Problem if parameter has space in it using loop

for cmdopts in $*;do case $cmdopts in -mode) mode="$2";shift 2 ;; -server) server="$2";shift 2 ;; -Id) Id="$2";shift 2 ;; -passwd) passwd="$2";shift 2 ;; -rmtDir) rmtDir="$2";shift 2 ;; -lcDir) ... (9 Replies)
Discussion started by: pinnacle
9 Replies

9. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

10. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies
Login or Register to Ask a Question