Visit Our UNIX and Linux User Community


awk stuff


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk stuff
# 1  
Old 08-21-2014
awk stuff

Hi,

My input file data will be
Code:
|ABCD|EFGH|IJKL|MNOP
|ABCD|EF\|GH|IJKL|MNOP

I am expecting output ,
Code:
|"ABCD"|"EFGH"|"IJKL"|"MNOP"
|"ABCD"|"EF|GH"|"IJKL"|"MNOP"

Note : The change basically the pipe deilmited file does contain | as value for some of the column but | will come with escape char \ always when it will be a value.

Please provide your ideas. i am looking for not only awk .. something different also welcome.

thanks in advance for your inputs

Last edited by Franklin52; 08-21-2014 at 09:52 AM.. Reason: Please use code tags
# 2  
Old 08-21-2014
Hi Nandy,

Please always use code tags as per forum rules for commands.
Following may help you in same.

EDIT: Edited the post as previous post was not giving the requested output. Thank you Scrutinizer for pointing out the same.
Not a best of the solution but will provide output for input provided by the user.

Code:
awk  '{gsub(/\\\|/,"#",$0);gsub(/\|/,"\"|\"");gsub(/^\"\|/,"|",$0);gsub(/$/,"\"",$0);gsub("#","|",$0); print $0}'   filename
OR
awk  '{gsub(/\\\|/,"#",$0);gsub(/\|/,"\"|\"");gsub(/^\"\|/,"|",$0);gsub(/$/,"\"",$0);gsub("#","|",$0)} 1'  filename

Output will be as follows.

Code:
|"ABCD"|"EFGH"|"IJKL"|"MNOP"
|"ABCD"|"EF|GH"|"IJKL"|"MNOP"


Thanks,
R. Singh

Last edited by RavinderSingh13; 08-22-2014 at 09:13 AM.. Reason: Edited as previous output was not requested one
# 3  
Old 08-21-2014
You could try something like:
Code:
sed '	s/\\|/@/g
	s/^@/"@/
	s/@$/@"/
	s/^|/@"/
	s/|$/"@/
	s/|/"|"/g
	s/[^"@]$/&"/
	s/^[^"@]/"&/
	s/@/|/g' file

which if file contains:
Code:
|ABCD|EFGH|IJKL|MNOP
|ABCD|EF\|GH|IJKL|MNOP
ABC|D\|\|E|LMN|
\|\|\|\|

produces the output:
Code:
|"ABCD"|"EFGH"|"IJKL"|"MNOP"
|"ABCD"|"EF|GH"|"IJKL"|"MNOP"
"ABC"|"D||E"|"LMN"|
"||||"

# 4  
Old 08-22-2014
Code:
awk '{gsub(/\|/, "\"|\"");gsub(/\\"\|"/,"|");$0=("\""$0"\"");sub(/(^"")|(""$)/,X)}1' file

# 5  
Old 08-22-2014
Assuming the first fields is empty / the file starts with a |

Bash:
Code:
while IFS=\| read -a fields
do
  unset fields[0]
  printf '|"%s"' "${fields[@]}"
  echo
done < file


---
General shell with 5 fields:
Code:
while IFS=\| read a b c d e
do
  printf '|"%s"' "$b" "$c" "$d" "$e"
  echo
done < file


Last edited by Scrutinizer; 08-22-2014 at 05:23 AM..
# 6  
Old 08-22-2014
Another awk
Code:
awk -F\| '{gsub(/\\\|/,"_");for (i=/^\|/?2:1;i<=(/\|$/?NF-1:NF);i++) $i="\""$i"\"";gsub(/_/,"|")}1' OFS=\| file
|"ABCD"|"EFGH"|"IJKL"|"MNOP"
|"ABCD"|"EF|GH"|"IJKL"|"MNOP"


Last edited by Jotne; 08-22-2014 at 08:39 AM..
# 7  
Old 08-22-2014
Still assuming the first fields is empty / the file starts with a |

Code:
awk '{for(i=1; i<=NF; i++) gsub(/\|/,"\"|\"",$i); sub(/"/,x); sub(/$/,"\"")}1' FS='\\\\\\|' OFS='|' file

Code:
perl -pe 's/(?<!\\)\|/"|"/g; s/\\\|/|/g; s/^"//; s/$/"/' file


---
@Jotne, Ravinder.. That output is not correct...

Last edited by Scrutinizer; 08-22-2014 at 08:46 AM..
This User Gave Thanks to Scrutinizer For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #567
Difficulty: Medium
The ternary if-then-else operator generally has the following syntax: E ? X : Y where E is a Boolean expression and if true, the statement is assigned the value Y.
True or False?

5 More Discussions You Might Find Interesting

1. Programming

More Arduino Stuff...

HI all... (Apologies for any typos.) To add to Neo's Arduino subject matter I have decided to upload this in ".zip" format. Ignore "*.info" files these are AMIGA icons only and also the "HAM" drawer as these are photos in ancient AMIGA HAM modes. I have noticed that there are current... (6 Replies)
Discussion started by: wisecracker
6 Replies

2. Shell Programming and Scripting

Please help with monitoring stuff

Hi, I am trying to write a script to do monitoring kind of stuff, requirement - when a server is given a start it updates a file called server.log, I need to keep on grepping the word "Running" and as soon as it comes , script should be exited with the message , "Server came up... (2 Replies)
Discussion started by: sunilmenhdiratt
2 Replies

3. Shell Programming and Scripting

A schoolboyish stuff

Hi , This is a pretty simple sed command i found when i was checking out one of the codes of my colleague . sed -e 's/\*.*\) \(\ <1*e\ >\) \(*.*\)/\2/' When i tried this on a few text files it was displaying the entire line. If this was to display entire line why sweat out on a sed . Does... (3 Replies)
Discussion started by: kinny
3 Replies

4. Windows & DOS: Issues & Discussions

weird stuff

I coudln't think of another topic to post this under as the OS on the system is XP pro. Ok here is the go. I'm upgrdaing a mates computer. A AMD 1200Mhz and well it wouldn't boot from the CD to do a fresh install (By upgrade I mean OS with complete new install). So I opened up the box and... (4 Replies)
Discussion started by: woofie
4 Replies

5. UNIX for Dummies Questions & Answers

Simple stuff.

I hacked my TIVO a few months ago. I made a computer specifically for this, but I only used a UNIX boot disk to get all the TIVO goodies to work. I am intersted in getting some version of UNIX on this machine and getting it onto my network. I only want to do some simple file transfers, maybe... (1 Reply)
Discussion started by: IamJAWA
1 Replies

Featured Tech Videos