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
# 1  
Old 11-09-2017
Delimiter appending in a data file if we receive a less columns than expected

Required No.of field = 12

Let say you got a “~” delimited input file and this file has 6 input fields and now I want to add 12-5=7 number of “~” into this input file in order to make it 12 fields

datafile can have n number of records

ex.,
Code:
a~b~c~d~12~r
a~b~c~d~12~r
a~b~c~d~12~r
a~b~c~d~12~r

output expected is
Code:
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~

which works in korn shell

Moderator's Comments:
Mod Comment Please:
- post in adequate forum
- use CODE tags as required by forum rules!

Last edited by RudiC; 11-09-2017 at 08:43 AM.. Reason: Moved to correct forum; added CODE tags.
# 2  
Old 11-09-2017
Welcome to the forum.

Any attempts / ideas / thoughts from your side?
# 3  
Old 11-09-2017
Hello LJJ,

Welcome to forums, please use code tags for your Input_file/commands/codes as per forum rules.
Could you please try following and let me know if this helps you.
Code:
awk -F"~" '{i=NF;while(i<14){val=val?val "~":"~";i++};print $0 val;val=""}'  Input_file

Thanks,
R. Singh
# 4  
Old 11-09-2017
Also, try:
Code:
$ cat file
a~b~c~d~12~r
a~b~c~d~12~r
a~b~c~d~12~r
a~b~c~d~12~r

$ awk -F~ 'NF < 12 { $12 = FS } 1' OFS=~ file
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~


Last edited by Scott; 11-09-2017 at 10:29 AM.. Reason: moved print outside action, otherwise ignores lines with >= 12 columns already
# 5  
Old 11-09-2017
Some awk versions allow for
Code:
awk -F"~" 'NF=12' OFS="~" file
a~b~c~d~12~r~~~~~~
a~b~c~d~12~r~~~~~~
a~b~c~d~12~r~~~~~~
a~b~c~d~12~r~~~~~~

This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-09-2017
Hi All thanks for the response .,

i'm bit surprised as none of the solns is producing the result is it because of the shell

trying some vague approach , please help
Code:
#The below gets the no of record delimiter in the data file
head -1 datagen.txt | awk -F'~' '{ print NF-1 }' > nrd.dat
###############33

nrd.dat was holding the no of delimiters we received in the data file


Code:
#!/bin/ksh
for i in `cat nrd.dat`; do
export df=`expr 10 - $i`
echo "$df"
export dlm='~'
echo "$dlm"
done
awk 'BEGIN { while (count++<14) string=string "~"; print string ;break}' > pt.dat


Last edited by Scott; 11-09-2017 at 10:36 AM.. Reason: Please use code tags
# 7  
Old 11-09-2017
There are a number of issues with your script, but that aside, I don't see any of the provided "solns" in your code.
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