Force cat to use OFS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Force cat to use OFS
# 1  
Old 10-13-2010
Force cat to use OFS

Hey, I am working on some file comparisons with csv files. csv files use "," as the field separator and I want to do a

Code:
cat -n file > file

to put the file numbers at the beginning of the lines, but cat uses "\t" as the separator. Can I force cat to use "," as the Output file separator. Or can i use

Code:
awk '{print getline}{print ","}{print $0}' file > file

# 2  
Old 10-13-2010
Same file for input and output won't work (some cat's actually complain if input and output files are the same):
Code:
$ cat -n FILE > FILE
cat: FILE: input file is output file

If you need to update file in-place try somehting like this
Code:
cat -n file | sed 's/\t/,/' > file2; mv file2 file

# 3  
Old 10-13-2010
cat doesn't care about the value of $OFS, as it just takes one file and prints it as-is on stdout.

But you can get a similar effect using perl -i.bak -ne 'printf "%d,%s", $., $_' file
# 4  
Old 10-14-2010
@pludi thanks for your excellent reply. That code worked great. Could you please explain how it works, in particular the
Code:
-i.bak -ne 'printf "%d,%s", $., $_'

part.

@Chubler_XL, I know cat dosn't like the same output and input stuff, my code on that line is actually a fairly long pipe getting input and then outputting it to a temp file. But thanks for the reply.

---------- Post updated at 12:30 AM ---------- Previous update was at 12:12 AM ----------

nevermind, I found a bash only alternative
here. (can't post urls until 5 posts!)
PHP Code:
head(www.unix.com/shell-programming-scripting/99016-how-add-line-numbers-text-file.html
Code:
awk '{printf "%s,%s\n",NR,$0}' FILE

# 5  
Old 10-14-2010
That last one isn't bash only, it's using awk instead of Perl.

Explanation of my code:
  • -i.bak means when changing the file contents, create a backup of the original using the suffix .bak
  • -n: loop over the contents of a file/stdin, but don't print by default
  • -e: execute a script given on the command line
  • the script will do a formatted print with 2 arguments, 1 digit and 1 string. The first argument is the current input line number ($.), the second is Perls default variable, which in this case is automatically set to the current input line ($_)
# 6  
Old 10-14-2010
Code:
awk '$0=NR","$0' infile

# 7  
Old 10-14-2010
@Scrutinizer

could you please explain the code..especially this part
Code:
'$0=NR

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