Read delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read delimited file
# 1  
Old 03-04-2015
Read delimited file

I have a delimited file (,) containing Name, Amount,Type,Address,zip,Tel and Extn. If any of this column information is missing (except TYPE and Extn),
I need to print that a spefic column value is missing in my output.

Example: row 2 is missing ZIP and the out put should contain NOZIP in the Zip column. How can write a code in Unix to process this file.

Input File
Code:
Name='ABC',Amount=89.56,TYPE=CREDIT,Address|GTFHNKJ,Zip|0765438765,Tel:8976544
Name='DET',Amount=56.42,TYPE=CREDIT,Address|SCVH,Tel:458962
Name='POJ',Amount=12.568,TYPE=CREDIT,Address|GTFHNKJ,Zip|256445,Tel:452365,Extn:5678
Name='TYI',Address|TFDSER,Zip|256445,Extn:1245

Output File
Code:
NAME,AMOUNT,ADDRESS,ZIP,TEL
ABC,89.56,GTFHNKJ,0765438765,8976544
DET,56.42,SCVH,NOZIP,458962
POJ,12.568,GTFHNKJ,256445,452365
TYI,NOAMOUNT,TFDSER,256445,NOTELEPHONE

Thanks..

Last edited by Don Cragun; 03-05-2015 at 03:00 PM.. Reason: Get rid of SIZE tags.
# 2  
Old 03-04-2015
You have not specified tools to use and what you tried so far, so I just use shell script. I don't want to do the complete work for you, but I will show how to loop through the lines of your file, how to break the line into comma-delimited columns, how to examine keyword and value of each column and show example of if statement, so you can do your data manipulation as you see fit.

Code:
#!/bin/sh
SAFE_FS=$IFS
IFS=',';
cat YOUR_FILE_NAME|while read col1 col2 col3 col4 col5 col6 col7;
do
        kwd=${col2%=*};
        val=${col2#=*};
        if [ $kwd = 'Amount' ]; then
                printf "column2<%s> keyword<%s> value<%s>\n" $col2 $kwd $val;
        else
                printf "column2<%s> this keyword<%s> Unexpected\n" $col2 $kwd;
        fi
done;
IFS=$SAFE_FS;

the output:
Code:
column2<Amount=89.56> keyword<Amount> value<Amount=89.56>
column2<Amount=56.42> keyword<Amount> value<Amount=56.42>
column2<Amount=12.568> keyword<Amount> value<Amount=12.568>
column2<Address|TFDSER> this keyword<Address|TFDSER> Unexpected


Last edited by migurus; 03-04-2015 at 08:46 PM.. Reason: added output
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

2. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

Hi Experts, I have a tab deliminated file as below myfile.txt Local Group Memberships *Administrators *Guests I need data in below format starting from 4th position. myfile1.txt Administrators Guests the above one is just an example and there could... (15 Replies)
Discussion started by: Litu1988
15 Replies

3. Shell Programming and Scripting

How to make tab delimited file to space delimited?

Hi How to make tab delimited file to space delimited? in put file: ABC kgy jkh ghj ash kjl o/p file: ABC kgy jkh ghj ash kjl Use code tags, thanks. (1 Reply)
Discussion started by: jagdishrout
1 Replies

4. Homework & Coursework Questions

Shell Script to read a tab delimited file and perform simple tasks

1. The problem statement, all variables and given/known data: Hello! I need help with this problem bash shell scripting that basically just reads the data in a tab delimited file and does the following below 1. Read in the data file Survey.txt and assign the column values to variables of... (6 Replies)
Discussion started by: jsmith6932
6 Replies

5. Shell Programming and Scripting

awk read one delimited file, search another delimited file

Hello folks, I have another doozy. I have two files. The first file has four fields in it. These four fields map to different locations in my second file. What I want to do is read the master file (file 2 - 23 fields) and compare each line against each record in file 1. If I get a match in all four... (4 Replies)
Discussion started by: dagamier
4 Replies

6. Shell Programming and Scripting

How to read nth word from delimited text file?

Hi i am new in scripting how i can get 2 elements from first line of delimited txt file in shell scripts. AA~101010~0~AB~8000~ABC0~ BB~101011~0~BC~8000~ABC~ CC~101012~0~CD~8000~ABC0~ DD~101013~0~AB~8000~ABC~ AA~101014~0~BC~8000~ABC0~ CC~101015~0~CD~8000~ABC~ can anyone plse help?... (3 Replies)
Discussion started by: sushine11
3 Replies

7. Shell Programming and Scripting

Read from Multiple character delimited flat file

Hello Guys I have a multiple character delimited flat file "|~|". when I tried to read the data the "|" character also coming Example I/P file 9882590|~|20111207|~|K03501000063005574033961|~|K|~| Command to get the second column I used awk -F"|~|" ' {print $2}' ... (2 Replies)
Discussion started by: Pratik4891
2 Replies

8. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

9. Shell Programming and Scripting

Read columns from delimited file in UNIX

Hello I need to read the columns from a flat file delimited by Hex code X02. The Sample file is Red^B1000^BJohn Blue^B2000^BSam Green^B3000^BDan Note: Hex code X02 shows as ^B in vi. I need to read the file and process the columns in each row. I tried using awk -F command but... (7 Replies)
Discussion started by: injey
7 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question