awk to print value from txt file to csv


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print value from txt file to csv
# 1  
Old 12-31-2011
Error awk to print value from txt file to csv

Hi,

I want to print two columns from a .txt file to a .csv file using awk.

data in text file:

Application
-------------------------------------------------- -----------
OS Related Issues 1
EMEA Solutions 9
Global Solutions 2


what i want to print is:

OS Related Issues,1
EMEA Solutions,9
Global Solutions,2

Please suggest some way to get this done. I'm new to shell scripting and m not really familiar with awk. Smilie

Thanks in advance.

FYI: working on ksh
# 2  
Old 12-31-2011
Code:
$ awk '{for(i=1;i<NF;i++){printf("%s ",$i)} printf(",%s\n",$NF)}' test.txt 
OS Related Issues ,1
EMEA Solutions ,9
Global Solutions ,2

---------- Post updated at 01:39 PM ---------- Previous update was at 01:25 PM ----------

Code:
$ awk '$NF=","$NF' test.txt
OS Related Issues ,1
EMEA Solutions ,9
Global Solutions ,2

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 01-03-2012
is there any way to skip the first two lines and display values from the 3rd line..??

the text file starts with:

Application
-------------------------------------------------- -----------

i want to remove the first two lines and get values from the 3rd line.. is that possible..??
# 4  
Old 01-03-2012
you can use

Code:
 
if ( NR >= 3 )

# 5  
Old 01-03-2012
If the additional blank is/was no problem you can try:
Code:
$> awk 'NR>2 {$NF=","$NF; print}' infile
OS Related Issues ,1
EMEA Solutions ,9
Global Solutions ,2

This User Gave Thanks to zaxxon For This Post:
# 6  
Old 01-03-2012
that worked fine.. one last question (i know i should have asked this earlier), how do i avoid the last line in that file..? sorry for bothering..
# 7  
Old 01-03-2012
Quote:
Originally Posted by prashu_g
that worked fine.. one last question (i know i should have asked this earlier), how do i avoid the last line in that file..? sorry for bothering..
With a little adjustment of zaxxon's code:
Code:
awk 's{print s} NR>2 {$NF=","$NF; s=$0}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need script to convert TXT file into CSV

Hi Team, i have some script which give output in TXT format , need script to convert TXT file into CSV. Output.TXT 413. U-UU-LVDT-NOD-6002 macro_outcome_dist-8.0.0(v1_0_2) KK:1.2.494 (1234:333:aaa:2333:3:2:333:a) 414. U-UU-LVDT-NOD-6004 ... (10 Replies)
Discussion started by: Ganesh Mankar
10 Replies

2. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

3. UNIX for Dummies Questions & Answers

How to create a .csv file from 2 different .txt files?

Hi, I need to create a .csv file from information that i have in two different tab delimited .txt file. I just want to select some of the columns of each .txt file and paste them into a .cvs file. My files look like: File 1 transcript_id Seq. Description Seq. Length ... (2 Replies)
Discussion started by: alisrpp
2 Replies

4. Shell Programming and Scripting

Converting txt file into CSV using awk or sed

Hello folks I have a txt file of information about journal articles from different fields. I need to convert this information into a format that is easier for computers to manipulate for some research that I'm doing on how articles are cited. The file has some header information and then details... (8 Replies)
Discussion started by: ksk
8 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. Shell Programming and Scripting

awk '{print $ from file1.txt}'

Hi All, I have a file1.txt where the index of the columns are placed. I want to get the columns from file2.txt corresponding to these index numbers. I was usually using awk '{print $5, $6, $2, $3, ...}' file2.txt > output.txt However, this list is very long. So, i want to read the... (4 Replies)
Discussion started by: senayasma
4 Replies

7. Shell Programming and Scripting

txt file to CSV

hi.. I have a text file which looks likes this 2258 4569 1239 258 473 i need to convert it into comma seperated format eg:2258,4569,1239,258,437 pls help (8 Replies)
Discussion started by: born
8 Replies

8. Shell Programming and Scripting

Converting txt file in csv

HI All, I have a text file memory.txt which has following values. Average: 822387 7346605 89.93 288845 4176593 2044589 51883 2.47 7600 i want to convert this file in csv format and i am using following command to do it. sed s/_/\./g <... (3 Replies)
Discussion started by: mkashif
3 Replies

9. Shell Programming and Scripting

Format txt file to CSV

Hi All, I have a file with content FLIGHT PLANS PRODUCED ON 26.08.2008(SORTED BY FPLAN NUMBER) RUN DATED 27/08/08 PAGE 1 -------------------------------------------------------------- FPLAN FPLAN PRE BTCH BATCH POST BTCH BATCH BATCH ... (1 Reply)
Discussion started by: digitalrg
1 Replies

10. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question