Print the file into column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print the file into column
# 1  
Old 11-19-2013
Print the file into column

Hi All,

Seeking for your assistance regarding on how to print multiple columns from file.

Ex.

nik.csv - input file
Code:
1,2,3,4,5,0054,May 1 
11,12,13,14,15,0053,May 2

the 6th field is i converted it into decimal

I create a variable on each field - pls see below.
Code:
T1=`awk -F "," '{print $1}' *.csv`
T2=`awk -F "," '{print $2}' *.csv`
T3=`awk -F "," '{print $3}' *.csv`
T4=`awk -F "," '{print $4}' *.csv`
T5=`awk -F "," '{print $5}' *.csv`
CONV=`echo "ibase=16; ${T6}" |bc

Expected Output:
1,2,3,4,5,84,May 1 - 84 means it's already converted to decimal
11,12,13,14,15,83,May 2 - 83 means it's already converted to decimal

my problem is i can't print it on my expected output. it will only print the whole $1 before it print the $2.

Please advise,

Thanks,
-nik

Last edited by Franklin52; 11-19-2013 at 05:29 AM.. Reason: Please use code tags
# 2  
Old 11-19-2013
one way:

Code:
mute@thedoctor:~/temp/nikki1200$ ./script
1,2,3,4,5,84,May 1
11,12,13,14,15,83,May 2
mute@thedoctor:~/temp/nikki1200$ cat script
#!/bin/sh

while IFS=, read a b c d e f g; do
        f=$(echo "ibase=16; $f" | bc)
        echo "$a,$b,$c,$d,$e,$f,$g"
done < "nik.csv"

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 11-19-2013
Try

Code:
cat <<eof | awk -F, '$6=strtonum("0x"$6)'
1,2,3,4,5,0054,May 1 
11,12,13,14,15,0053,May 2
eof

1 2 3 4 5 84 May 1 
11 12 13 14 15 83 May 2

for file use like this to keep comma
Code:
$ awk -F, '$6=strtonum("0x"$6)' OFS=\, file

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 11-19-2013
Thanks Everyone for the support.
# 5  
Old 11-19-2013
Hi,
In gnu env (sed+bash):
Code:
$ sed -r 's/(([^,]*,){5})([^,]*),/echo \1$[16#\3],/e' nik.csv
1,2,3,4,5,84,May 1
11,12,13,14,15,83,May 2

Regards.
This User Gave Thanks to disedorgue For This Post:
# 6  
Old 11-19-2013
Perl...
Code:
perl -F"," -lnae '$F[5]=hex($F[5]);print join (",",@F);' filename

This User Gave Thanks to pravin27 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

Print a column with the name of the output file

I have n files and I am using cat to combine them in to one. Before that simply add the name of the output file to 4th column and then print the output. Is it possible ? input1 chr start end name 0 + key input2 chr start end name 0 + key inputn... (1 Reply)
Discussion started by: quincyjones
1 Replies

4. Shell Programming and Scripting

print when column match with other file

Hello all, please help. There are two file like this: file1: 1197510.0 294777.7 9666973.0 21.6 1839.8 1197510.0 294777.7 9666973.0 413.2 2075.9 1197510.0 294777.7 9666973.0 689.3 2260.0 ... (1 Reply)
Discussion started by: attila
1 Replies

5. Shell Programming and Scripting

Need to find a column from one file and print certain columns in second file

Hi, I need helping in finding some of the text in one file and some columns which have same column in file 1 EG cat file_1 aaaa bbbb cccc dddd eeee fffff gggg hhhh cat file_2 aaaa,abcd,effgh,ereref,name,age,sex,........... bbbb,efdfh,erere,afdafds,name,age,sex.............. (1 Reply)
Discussion started by: jpkumar10
1 Replies

6. Shell Programming and Scripting

How to print file without first column

Hi, unix Gurus, I hit a problem, I want print out a file without print first column. for example: 123 abc cde ... 234 cde def ... I want to get as following: abc cde .. cde def .. :wall: anybody can help me out. Thanks in advance. (8 Replies)
Discussion started by: ken002
8 Replies

7. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

8. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

9. Shell Programming and Scripting

How do you print column 1 from comm output to a file?

Hi guys, I have a script, which after running for 20 minutes, produces a bunch of IPs. Due to a DHCP scope, some of these IPs are not useable, so I would like to eliminate them from the final list. I have used comm to do this, but am unable to extract the first column, and redirect it to a... (1 Reply)
Discussion started by: Bloke
1 Replies

10. Shell Programming and Scripting

Conditional aggregation and print of a column in file

Hi My input file looks like field1 field2 field3 field4 field5 field1 field2 field3 field4 field5 field1 field2 field3 field4 field5 :::::::::::: :::::::::::: There may be one space of multiple spaces between fields and no fields contains spaces in them. If field 1 to 4 are equal for... (3 Replies)
Discussion started by: bittoo
3 Replies
Login or Register to Ask a Question