Force cat to use OFS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Force cat to use OFS
# 8  
Old 10-14-2010
It means: replace $0 with the line number (NR) followed by a comma and $0. Since the resulting outcome is never zero or "", this means that $0 get printed subsequently.
# 9  
Old 10-14-2010
@pludi Well, ok. I know it's not bash only, but what i meant was that awk is standard in the UNIX kernel, so it's a standard kernel solution. I don't have to make anyone running this script install perl.
# 10  
Old 10-14-2010
Code:
cat -n infile | sed 's/\t/,/g;s/\s//gw infile'

cat -n infile | sed 's/\t/,/g;s/\s//g' >infile

@Chubler : no need of temporary file2 no need for mv either.
# 11  
Old 10-14-2010
Quote:
Originally Posted by MHardeman25
what i meant was that awk is standard in the UNIX kernel, so it's a standard kernel solution.
Can you elaborate this part please Smilie
# 12  
Old 10-14-2010
@danmero. lol. At least I don't think perl has been added to the kernel yet.
# 13  
Old 10-14-2010
@MHardeman25: Neither has awk. But both are shipped with most current UNIX variants, and installed by default.
# 14  
Old 10-14-2010
A fork-exec per line is not a cheap solution. I ran a test just to see what cat -n did with line numbers > 999999, and they push out to the right losing right justification, no big surprise. I was surprised that 'cat -n' took more CPU than either the data generator or the sed after it to toss the low lines!

I see the 'cat -n' leading spaces=6 digit right justificating as overreaching: too paper oriented too soon, like 'uniq -c' but not agreeing on the number column width! I used to do it all with sed's: no spaces/right justification, more code lines but I love readability, modularity and reusable simple pieces:
Code:
sed '
  #
 ' file_in |sed '
  N
  s/\n/,/
 ' > file_out

I wrote a general C tool to right justify columns if I want that, in dynamically sized columns: autotab.
Code:
$ autotab --help

Usage: autotab [ -is <i_sep> ] [ -os <o_sep> ] [ -gh ] [ -no ] [ -j <just> ]

Scans input as columns defined by <i_sep> (default tab), measuring maximum
column width without blank padding and saving the input.  Lines with no
<i_sep> are not measured.  If -no is present (narrow, overlapping), the
characters between the last <i_sep> on a line and the line feed are not
measured.  (The -no option is only useful with left justification.)
After reading EOF, the saved input is printed, padded to the measured
column width and separated by the <o_sep> string (default 2 spaces) with
empty right side columns, blanks and carriage returns suppressed.
If -j is present, the characters of <just> define the justification of each
column with the same relative offset:
 r for right, c for centered, and anything else means left.
If -gh is present, the saved input is prefixed by a numbered column header,
which is padded and aligned like the data.
The size limits are: 4096 measured columns, output line 65536 characters
and right or center justified column data width 4096 characters.

$

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

OFS in awk

Hello, I have an issue with adding commas as delimiters in this scenario: cat xtr3.rpl|head -5|awk 'BEGIN {OFS=","} {print $1,$2,$3,$4}' Produces this output: 00530083,0000000471,000000000000.00,000000000000.00 00530085,0000000471,000000000000.00,000000000000.00... (10 Replies)
Discussion started by: MIA651
10 Replies

2. Shell Programming and Scripting

Awk OFS issues

Hi Im trying to tidy up the output of a who command when it writes to a log, everything I've tried doesnt seem to work though, any help would be massively appreciated. Im using the awk command to set the OFS as tab. #!/bin/bash who >> /export/home/tjmoore/logusers awk -F 'BEGIN... (3 Replies)
Discussion started by: 02JayJay02
3 Replies

3. Shell Programming and Scripting

AWK - OFS

Hi All, I have a comma seperated delimited file with 10 columns. I need to convert it into TAB seperated delimited file. awk -F"," '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10}' a.txt >> b.txt how to use OFS to get the same output. I have tried by googling, but it... (5 Replies)
Discussion started by: Amit.Sagpariya
5 Replies

4. Shell Programming and Scripting

Using of OFS

Hey guys, i am having a slight problem with OFS. I have a database which displays its information like this , John:22:345 I need to read an input from the user and then replace it in $1. awk -F":" '{ $1 = "'$input'" ; print $0 } ' fruittemp > fruittemp2 mv fruittemp2 fruittemp the... (1 Reply)
Discussion started by: gregarion
1 Replies

5. Shell Programming and Scripting

OFS in awk.

OFS is inbuild command in awk. I have a file file.txt abc : def : ghi jkl : mno: pqr stu : vwx :yzz code i used: awk -F ":" 'BEGIN {OFS="|"} {print $1,$2}' file.txt output: abc def jkl mno stu vwx but as i have used OFS="|" and i am expecting output as: abc | def jkl... (4 Replies)
Discussion started by: salil2012
4 Replies

6. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

7. Shell Programming and Scripting

Retaining default OFS

Hi, I wat to remove the first two columns of the ls -l command: $ ls -l | awk '{ $1="";$2="";print;}' The only problem is now the columns of the output is space separated instead of the separators the ls -l originally throws. Can someone tell how to preserve the default OFS of an ls -l... (3 Replies)
Discussion started by: amicon007
3 Replies

8. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

9. UNIX for Dummies Questions & Answers

OFS in awk

Hi, I have these out put field seperator changed to "|" in my awk command, but it didn't give me the result. Can someone help me find out why? ======================================= /bin/awk 'BEGIN { OFS="|" } { print $0 }' list.tmp.$$ > listtmp.$$ =======================================... (1 Reply)
Discussion started by: whatsfordinner
1 Replies
Login or Register to Ask a Question