Delimiter appending in a data file if we receive a less columns than expected


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Delimiter appending in a data file if we receive a less columns than expected
# 15  
Old 11-09-2017
A slightly different way that should work with all versions of (n)awk :
Code:
awk '{$12=$12}1' FS=\~ OFS=\~ file




--
or, more general:
Code:
awk -v n=12 '{$n=$n}1' FS=\~ OFS=\~ file

This User Gave Thanks to Scrutinizer For This Post:
# 16  
Old 11-10-2017
Thanks for the response it works for the '~' delimiter ., but for '|' this is appending from the front replacing the characters .

Code:
/data/files/jlj > nawk -v n=12 '{$n=$n}1' FS=\| OFS=\| dtd.dat > p.dat
/data/files/jlj > cat p.dat
|||||||Brennan|Shaine|Lane Avila|Graham@dignissim.org|658
|||||||bles|Nyssa|Hop Montoya|Hermione@adipiscing.com|769
||||||| Mckinney|Jana|Ashely Perkins|Meghan@rhoncus.com|522
|||||||Mccarthy|Gage|Kiara Solomon|Cathleen@ornare.us|462

Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules) when displaying sample input, output, and code segments.

Last edited by Don Cragun; 11-10-2017 at 03:37 AM.. Reason: Add CODE tags again.
# 17  
Old 11-10-2017
Without showing us what input you had that produced that output, we have to assume that your input had leading vertical bars. When I try the code you showed us in post #16 with the input:
Code:
Brennan|Shaine|Lane Avila|Graham@dignissim.org|658
bles|Nyssa|Hop Montoya|Hermione@adipiscing.com|769
 Mckinney|Jana|Ashely Perkins|Meghan@rhoncus.com|522
Mccarthy|Gage|Kiara Solomon|Cathleen@ornare.us|462

the output I get is:
Code:
Brennan|Shaine|Lane Avila|Graham@dignissim.org|658|||||||
bles|Nyssa|Hop Montoya|Hermione@adipiscing.com|769|||||||
 Mckinney|Jana|Ashely Perkins|Meghan@rhoncus.com|522|||||||
Mccarthy|Gage|Kiara Solomon|Cathleen@ornare.us|462|||||||

What shell are you using?
# 18  
Old 11-10-2017
Code:
/data/files/jlj > echo $0
-bash
/data/files/jlj > uname -a
SunOS ux562z3 5.10 Generic_150400-17 sun4u sparc SUNW,SPARC-Enterprise


Last edited by Don Cragun; 11-10-2017 at 04:30 AM.. Reason: Add CODE tags again.
# 19  
Old 11-10-2017
That may be caused by a file in Windows format. Convert it to Unix-style first:

Code:
tr -d '\r' <dtd.dat >dtd.unix.dat

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 20  
Old 11-10-2017
thank you Smilie,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to loop file data based on delimiter

My file has data that looks like below: more data.txt I wish to display each string seperated by a delimiter : Expected output: I tried the below but I m not getting every split string on a new line. #!/bin/bash for i in `sed 's/:/\\n/g' data.txt`; do echo -n... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Help with data appending to a file

Hi I have a file called text.txt contains x y z when i run a command i will get output like below x 20 z 30 i want to insert x, z value in text.txt file and should be like this x 20 y 0 z 30 can anyone help me please? (1 Reply)
Discussion started by: siva kumar
1 Replies

3. Shell Programming and Scripting

Help- counting delimiter in a huge file and split data into 2 files

I’m new to Linux script and not sure how to filter out bad records from huge flat files (over 1.3GB each). The delimiter is a semi colon “;” Here is the sample of 5 lines in the file: Name1;phone1;address1;city1;state1;zipcode1 Name2;phone2;address2;city2;state2;zipcode2;comment... (7 Replies)
Discussion started by: lv99
7 Replies

4. Shell Programming and Scripting

appending data to last line of file

A friend contacted me recently with an interesting question. We got something worked out, but I'm curious what answers you all can come up with. Given a shell script (in bash) that processes a bunch of data and appends it to a file, how would you append the date, time, and a filename to the... (6 Replies)
Discussion started by: malcolmpdx
6 Replies

5. Shell Programming and Scripting

appending data to file

Hi. I wrote a very simple script and it doesn't work :( It is supposed to go to a certain directory, execute some command and append the output to the file "expo.dat" what it does is that it writes to the file only one entery. I dont know if Im using the write synthax for "append". Here is... (3 Replies)
Discussion started by: Enigma08
3 Replies

6. Shell Programming and Scripting

append data in a file by using tab delimiter

Hi, I need to append the data in to a file by using tab delimiter. eg: echo "Data1" >> filename.txt echo "\t" >> filename.txt (its not working) echo "Data2" >> filename.txt. the result sould be like this. Data1 Data2 (6 Replies)
Discussion started by: Sharmila_P
6 Replies

7. Shell Programming and Scripting

how to differentiate columns of a file in perl with no specific delimiter

Hi everybody, This time I am having one issue in perl. I have to create comma separated file using the following type of information. The problem is the columns do not have any specific delimiter. So while using split I am getting different value. Some where it is space(S) and some where it is... (9 Replies)
Discussion started by: Amiya Rath
9 Replies

8. Shell Programming and Scripting

Appending data at the first and last line of a file

Hi, Am trying to write a shell script which will append a header and a footer to an existing file. Header will contain details like the current date while the footer will contain the no: of records listed in the file. I know we can use the CAT command, but i have no clue abt the syntax to... (4 Replies)
Discussion started by: brainstormer
4 Replies

9. Shell Programming and Scripting

replace delimiter : with '\001' in unix data file

HI can any one tell me how to replace a delimiter : with another delimiter '\001' it is a non printable octal character. thanks in adv spandu (4 Replies)
Discussion started by: spandu
4 Replies

10. Shell Programming and Scripting

Appending columns on a file

My issue is the following: I have several text files, let's say 10 of them. Each one has three columns separated by a tab: Date, Time and Value. What I want to do next is to have only one text file containing the information: Date, Time, Value1, Value2, Value3, ... , Value10, where Value1... (2 Replies)
Discussion started by: abel
2 Replies
Login or Register to Ask a Question