awk - Must "touch" a $n-variable to get OFS used?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Must "touch" a $n-variable to get OFS used?
# 1  
Old 07-18-2012
awk - Must "touch" a $n-variable to get OFS used?

I'm having a small problem with awk. I tried to use it for replacing ";" with <tab>. But I have to "touch" one variable to get the <tab> in OFS to be used.
Code:
$ echo "xx;yy;zz" | awk -F';' 'BEGIN { OFS="\t" } { print $0; }'
xx;yy;zz                # no OFS used
$ echo "xx;yy;zz" | awk -F';' 'BEGIN { OFS="\t" } { print $0; print $1,$2}'
xx;yy;zz
xx      yy              # only OFS used for "field for field"
$ echo "xx;yy;zz" | awk -F';' 'BEGIN { OFS="\t" } { print $0; print $1,$2; print $0}'
xx;yy;zz
xx      yy
xx;yy;zz                # still no tab for $0
$ echo "xx;yy;zz" | awk -F';' 'BEGIN { OFS="\t" } { print $0; print $1,$2; print $0; $1=$1; }'
xx;yy;zz                # tested to "touch" $1 at the end, no success
xx      yy
xx;yy;zz
$ echo "xx;yy;zz" | awk -F';' 'BEGIN { OFS="\t" } { print $0; print $1,$2; print $0; $1=$1; print $0}'
xx;yy;zz
xx      yy
xx;yy;zz
xx      yy      zz      # ta-dam! here is OFS used, AFTER the "touch"
# and here is a "funny" result, in the first line OFS is used, but not for $0:
$ echo "xx;yy;zz" | awk -F';' 'BEGIN { OFS="\t" } { print "",$0; $1=$1; print "",$0 }'
        xx;yy;zz
        xx      yy      zz

I have googled but haven't find any information. Am I missing something? Or I have to do this:
Code:
echo "xx;yy;zz" | awk -F';' 'BEGIN { OFS="\t" } { $1=$1; print }'

Shell: bash, system: FreeBSD (is that correct info?)
# 2  
Old 07-18-2012
Wouldn't it be better to use sed?

Code:
echo "xx;yy;zz" | sed 's/;/\t/g'

# 3  
Old 07-18-2012
This goes round and round... Smilie
In this thread
Quote:
Originally Posted by 244an
...
... I was first thinking of an awk-solution, but because of the problem described here I suggested a sed-solution like yours - but you must use a "real" tab instead of "\t". The other thread is about that problem. Either you haven't tested your solution, or your system works different.

PS. The problem is not how to solve this, I want to know how awk works.

Last edited by 244an; 07-18-2012 at 07:26 PM..
# 4  
Old 07-18-2012
Quote:
Originally Posted by 244an
The problem is not how to solve this, I want to know how awk works.
The original record, $0, is not modified by the awk field splitting process nor is it modified by setting the value of OFS. When you print $0, it is printed as is, regardless of the value of OFS.

OFS is used in two situations:
1. To separate the print statment's arguments.
2. As the field separator when $0 needs to be rebuilt, which happens when a field is assigned a value (this includes the use of the sub/gsub functions) or when you assign to a new field beyond the current value of NF.

The awk standard document: http://pubs.opengroup.org/onlinepubs...ities/awk.html

Regards,
Alister

Last edited by alister; 07-18-2012 at 08:07 PM..
# 5  
Old 07-19-2012
Quote:
Originally Posted by 244an
This goes round and round... Smilie
In this thread ... I was first thinking of an awk-solution, but because of the problem described here I suggested a sed-solution like yours - but you must use a "real" tab instead of "\t". The other thread is about that problem. Either you haven't tested your solution, or your system works different.

PS. The problem is not how to solve this, I want to know how awk works.

Hi

The \t certainly works on Ubuntu using the bash shell
# 6  
Old 07-19-2012
Quote:
Originally Posted by steadyonabix
The \t certainly works on Ubuntu using the bash shell
Whether \t is supported or not by sed depends only on the sed implementation itself. The shell is irrelevant. The operating system is irrelevant (except as an indication of which sed implementation is provided by default).

I believe that the only commonly-used sed that supports \t in either portion (regular expression or replacement text) of a substitution command is GNU sed.

According to the document standardizing sed (and the regular expression standard to which it refers), \t is an undefined sequence. When encountered, an implementation could treat it as a tab (as GNU sed does), it could treat it as a literal backslash followed by the letter tee (as just about every other sed does), it could abort with a syntax error, or it could choose to delete a file (though this is extremely unlikely Smilie). The same is true of any sequence consisting of a backslash followed by an ordinary character, except for ...

Quote:
* The characters ')', '(', '{', and '}'
* The digits 1 to 9 inclusive (see BREs Matching Multiple Characters)
* A character inside a bracket expression
and

Quote:
The escape sequence '\n' shall match a <newline> embedded in the pattern space. A literal <newline> shall not be used in the BRE of a context address or in the substitute function.
Source of the 1st quote, which applies to all basic regular expressions: Regular Expressions
Source of the 2nd quote, which applies to sed regular expressions : sed

Long story short, if you use \t in a sed command, it will only work with GNU sed, which practically means Linux-only. Don't expect such scripts to run as intended on non-Linux platforms.

Regards,
Alister

Last edited by alister; 07-19-2012 at 10:23 AM..
This User Gave Thanks to alister For This Post:
# 7  
Old 07-19-2012
Good to know
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Permission error when "touch"ing file with different user

Hi, There are 2 users (T886072 & T864764) that need to be provided full (rwx) access to a directory. I made the changes to the directory permissions using chmod and setfacl : root@digidb2:# chmod 700 /u02/ftpfiles/MFRS16/discount_rates/ root@digidb2:# setfacl -s... (3 Replies)
Discussion started by: anaigini45
3 Replies

2. Shell Programming and Scripting

awk variable into shell command "date -d": possible...?

Hello, there! I am trying to pass an awk variable into a shell command in order to collect the result into an awk variable; in Bash it does work, as in: v='2'; date -d "now + $v weeks" But in awk it does not, as in: v="2" "date -d 'now + v weeks'" | getline newdate close ("date -d 'now... (3 Replies)
Discussion started by: fbird3
3 Replies

3. Shell Programming and Scripting

Capture the last record number using "awk" NR variable

Hi Team. I am trying to capture the last record number from a file using the below command ( assuming abc.txt has 21 records and I want 21 as output ) awk'{c=NR;print c}'abc.txt But it is printing all the record number. Can someone please help modify the above command? (8 Replies)
Discussion started by: chatwithsaurav
8 Replies

4. Shell Programming and Scripting

Variable interpolation in "print" of awk command

Hi, I have a snippet like below. Based on variable i, i wish to print 1,2,3,4,5th columns to Sample files. For each loop, one column from contetn and results will be pused to sample files. But i have a problem here i=1 while ; do `awk -F"\t" '{print $($i)}' $content > Sample_${i}_original`;... (4 Replies)
Discussion started by: forums123456
4 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

Can I make "touch" create executable files by manipulating umask?

I'm getting to grips with this concept of the umask. What I thought was, setting umask uga+rwx would result in creating files with all permissions for everyone. Seems not to be the case though. Read and write bits get set, but not the execute bit. Is there some gap in my understanding, or is... (2 Replies)
Discussion started by: tphyahoo
2 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Advanced & Expert Users

Command similar to "touch" for modify File size

Hi All, I'm trying to find a command like similar to "touch" which would let me change the file size property. For ex: I have a file of size 1MB using the command i would like to set/update the size something like 1KB. Is it possible? Is there any such command which would accomplish this... (3 Replies)
Discussion started by: sriharshareddyk
3 Replies
Login or Register to Ask a Question