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
# 8  
Old 11-09-2017
Are you on Solaris? If so use /usr/xpg4/bin/awk instead
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 11-09-2017
Quote:
Originally Posted by LJJ
Hi All thanks for the response .,

i'm bit surprised as none of the solns is producing the result . . .
Apart from the fact that in your post#1 the spec ("No.of field = 12") doesn't match the desired result (13 fields) ALL solutions proposed produce an easily adaptable result that should satisfy your request.
# 10  
Old 11-09-2017
Quote:
Originally Posted by RavinderSingh13
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
Hi R.Singh ., kindly help what does {val=val?val "~":"~";i++} does ?

---------- Post updated at 08:29 PM ---------- Previous update was at 08:24 PM ----------

Quote:
Originally Posted by Scott
There are a number of issues with your script, but that aside, I don't see any of the provided "solns" in your code.
Dear scott ., i tried the same example to execute ., and it returned the same data output, any idea what could be issue

Code:
:/data/files/jlj > cat sample.dat
a~b~c~d~12~r
a~b~c~d~12~r
a~b~c~d~12~r
a~b~c~d~12~r
:/data/files/jlj > awk -F"~" 'NF < 12 { $12 = FS; print }' OFS="~" sample.dat
a~b~c~d~12~r
a~b~c~d~12~r
a~b~c~d~12~r
a~b~c~d~12~r
:/data/files/jlj > awk -F~ 'NF < 12 { $12 = FS; print }' OFS=~ sample.dat
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 11:04 AM.. Reason: last output had a difft output; Scott: Added code tags
# 11  
Old 11-09-2017
Then, if you are using Solaris, refer to Scrutinizer's post. Use /usr/xpg4/bin/awk or nawk.
# 12  
Old 11-09-2017
Quote:
Originally Posted by LJJ
Hi R.Singh ., kindly help what does {val=val?val "~":"~";i++} does ?
Hello LJJ,

Could you please go through following explanation and let me know if this helps you.
Code:
awk -F"~" '{          ##Making field separator as ~ here for each line of Input_file.
i=NF;                 ##creating a variable named i whose value is equal to value of NF, NF is out of box variable which defines number of fields in a line of Input_file.
while(i<14){          ##Initiating while loop here which will run from i value from 0 to 13 here.
  val=val?val "~":"~";##creating a variable named val whose value will be having its value to ~ on very first occurrence and will concatenate itself each time with its previous value. Where ? means if condition is TRUE then execute statement followed by it or condition is FALSE then execute statement after :
  i++                 ##incrementing the value of variable named i here.
};
print $0 val;         ##printing current line and value of variable val.
val=""                ##nullifying value of val here.
}' Input_file         ##Mentioning Input_file here.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 13  
Old 11-09-2017
Quote:
Originally Posted by Scrutinizer
Are you on Solaris? If so use /usr/xpg4/bin/awk instead
Code:
/data/files/jlj > uname -a
SunOS ux562z3 5.10 Generic_150400-17 sun4u sparc SUNW,SPARC-Enterprise

---------- Post updated at 08:43 PM ---------- Previous update was at 08:39 PM ----------

Quote:
Originally Posted by Scott
Then, if you are using Solaris, refer to Scrutinizer's post. Use /usr/xpg4/bin/awk or nawk.
Hey ., Thanks Scott .,

it worked ! it worked !
Code:
/data/files/jlj > nawk -F"~" 'NF < 12 { $12 = FS; print }' O
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~
a~b~c~d~12~r~~~~~~~


Thank u so much

Last edited by Scott; 11-09-2017 at 11:18 AM.. Reason: Code tags, please...
# 14  
Old 11-09-2017
Cool! As RudiC pointed out, what you asked for will actually give you thirteen fields. You want to reduce those numbers by 1 to get the twelve you desired (or use RudiC's solution, which gives the desired result already (although this didn't work for me, it might for you - but on Solaris, who knows? 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