I need a help from experts of this community regarding one of the issue that I am facing with shell scripting.
My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line.
The script that I have written append almost all char but I am facing a problem with appending \t or \n when using the variable concept. However, if I hardcode \t and \n in the scripts , its working fine.
The sample file (TESTDATA.txt) that I have created is as shown below :
you dont put shell variables inside of awk programs, at least not like that. they're different and separate things. you can pass variables to awk like this: awk -v "rsuffix=$RSUFFIX" and then use it as rsuffix .
Thanks for your reply ..
Eventhough I used "$RSUFFIX" , I am not seeing any change in my output i.e. no char are appended to the last line of each record.
Below is my current code and I am executing the following , from the command line ( ./SampleScript.sh '\\t' )
SampleScript.sh ->
#!/bin/ksh
#================================#
# Script to change the #
# delimiter of any file and #
# also zip the file #
#================================#
NOW=`date '+%Y%m%d%H%M'`
NOW_DATE=`date '+%Y%m%d'`
RSUFFIX=$1
echo "==============================="
echo "$NOW"
echo "$RSUFFIX"
echo "===================="
rm TEMP1.txt
rm TEMP2.txt
touch TEMP1.txt
touch TEMP2.txt
echo "$RSUFFIX"
nawk -v rsuffix="$RSUFFIX" 'BEGIN { print rsuffix }'
##======== This doesn't work =============================
awk '{print $0 rsuffix}' TESTDATA.txt > TEMP1.txt
##======== This works =============================
awk '{print $0 "\t\t"}' TESTDATA.txt > TEMP2.txt
x='\t'
awk -v x="$x" 'BEGIN { printf "%s", x }' | od -c
0000000 \t
0000001
Also, if you are already single-quoting, there's no need to double the backslash. Doing so yields two characters, a literal backslash followed by the letter tee, instead of the escape sequence that produces a single tab character.
x='\\t'
awk -v x="$x" 'BEGIN { printf "%s", x }' | od -c
0000000 \ t
0000002
Alister,
Thanks for your reply but I am unable to correlate your solution with my requirement.. Can you please explain as how I will incorporate your solution to my requirement?
##======== This doesn't work =============================
awk '{print $0 rsuffix}' TESTDATA.txt > TEMP1.txt
##======== This works =============================
awk '{print $0 "\t\t"}' TESTDATA.txt > TEMP2.txt
In the second line , I have hardcoded the tab and I can see the results in TEMP2.txt . Ideally , if the solution worked , then TEMP1.txt and TEMP2.txt should have the same data and structure
vgersh99,
Thanks for your response. The command line appened two tabs at the end of each line. Can you please explain as how the statement works?
Also, the \t\t that you have hardcoded will be parameterized in my case. Can I replace \t\t using a parameter that I will be passing through command line?
In your script, rsuffix is empty, and so I don't expect it to output a <TAB> char. Try to assign sth. to it first, like rsuffix="\t" if your awk allows for that.
@neutronscott : Thanks for your solution. It worked. @vgersh99 : Its not working. May be because you are not mentioning the existing fileseperator in the script