Need a ksh script for adding the space at the end of record in a flat file

Hi,

I need a ksh script for the below requirement:
i have a Delimited flat file with 200 records delimiter is '|~|'
i need a script to insert space at the end if the record is ending with delimiter '|~|'
if it didnt end with delimiter it should not append space.

Example: ram|~|2|~| ---space should be appended
shyam|~|2|~|4 ----space should not be appended

please help me with this i need the complete script.

Thank you!

Try:

awk '/\|~\|$/{$0=$0" "}1' file

Hi,

Can you please give me the script,because i didnt know how to read the flat file and rotate the loop for each and every record in the flat file

This will do all those things. Try running:

awk '/\|~\|$/{$0=$0" "}1' file > new_file

and then compare file and new_file .

1 Like

Just to get the requirement right, are you looking to create a fixed width output file?

If you have the data you suggest:-

ram|~|2|~|
shylam|~|2|~|4

... then you could pass this through sed to add trailing space, but how much do you want? A single character perhaps or pad to a fixed length?

Hoping I can help,
Robin
Liverpool/Blackburn
UK

Hi,

I just want one single space if the records ends with a delimter.
if the record doesnt end with delimter nothing should be appended

Thanks!

Ha, I'm beaten to it with the awk

A very good answer. Does it make sense?

Robin

Another approach:

sed 's/|$/| /' file
1 Like

@bartus11: that command is giving error
code:

=> awk '/\|~\|$/{$0=$0" "}1' file5
awk: syntax error near line 1
awk: bailing out near line 1

Use nawk or /usr/xpg4/bin/awk on Solaris.

1 Like

@Franklin52: whats the basic difference between awk and nawk, why was awk not working on solaris??

Thanks,
Aditya

nawk ("new awk") is an extended version of awk with some features the original awk lacked. Franklin52 used one of these features and this is why the standard awk failed.

I hope this helps.

bakunin

Thanks for solution and your explanations.

Hi,

Thanks bartus11 for the very useful information

can you please expalin me how this highlighted line is working....?

awk '/\|~\|$/{$0=$0" "}1' file > new_file

/\|~\|$/ -> checking this pattern is there in the line (ends with |$), here $ indicates "end of the line"

$0=$0" " --> if the above condition is true, then it add single space to your line.

}1 --> it will print the line ( modified line, as well as unmodified line - pattern not matched )

1 Like

Hi,

I have a situation like

awk '/\|~\|$/{$0=$0" "}1'  filename.txt > output.txt

in the above code filename.txt is coming from other server

so i cant use this line in a generic script as the filename was constantly changing

can you please suggest how can i proceed in this......

Thank you!

Use a shell variable.