Read file contents and separate the lines when completes with =


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read file contents and separate the lines when completes with =
# 1  
Old 12-14-2010
Read file contents and separate the lines when completes with =

Hi,

I have a file like this
Code:
cpsSystemNotifyTrap='2010/12/14 11:05:31 CST' Manufacturer=IBM ReportingMTMS=n/a ProbNm=26 LparName=n/a FailingEnclosureMTMS=7946-IQL*99G4874 SRC=B3031107 EventText=Problem reported by customer. CallHome=true Calendar

I want to have a output like this
Code:
cpsSystemNotifyTrap='2010/12/14 11:05:31 CST' 
Manufacturer=IBM 
ReportingMTMS=n/a
ProbNm=26 
LparName=n/a
FailingEnclosureMTMS=7946-IQL*99G4874 
SRC=B3031107 EventText=Problem reported by customer.
CallHome=true Calendar

Any code how to do it with awk or sed. or any other alternative?

Regards,
Dinesh

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

I copied one of the solution given in this forum but does not seems to work

Code:
#!/usr/bin/sh
set -x
while IFS='&' read -a L
do
for i in ${!L[@]}; do
eval "${L[$i]}"
done
for V in cpsSystemNotifyTrap Manufacturer ReportingMTMS ProbNm LparName FailingEnclosureMTMS SRC EventText CallHome
do
echo -en "$V=${!V}\t"
done
echo
done < /opt/Tivoli/custom/bin/trap.txt

Moderator's Comments:
Mod Comment Code tags, please

Last edited by pludi; 12-14-2010 at 05:43 AM.. Reason: Please use code tags, thank you
# 2  
Old 12-14-2010
Try
Code:
perl -n -e '@val = split(/(\w+)=/);foreach ( @val ) {if ( $_) {if ($i && $_) {print $val."=".$_ ."\n";$i=0 }else {$val=$_;$i++;}}}' infile

# 3  
Old 12-14-2010
Another Perl:
Code:
perl -anF"=" -e '$\="=";for (@F){s/ ([^ ]+)$/\n$1/;print}' file

# 4  
Old 12-14-2010
Another one:
Code:
awk '{
  printf $1
  for(i=2;i<=NF;i++) {printf("%s%s",$i ~ /=/?RS:FS,$i)}
  printf RS
}' file

# 5  
Old 12-14-2010
Code:
 
sed '/s\( [a-zA-Z][a-zA-Z0-9]*\)=/FSXXXX\1=/g' /opt/Tivoli/custom/bin/trap.txt | awk -F "FSXXXX " 'BEGIN{OFS="\n";}{for(i=1;i<=NF;i++)print $i;}'

# 6  
Old 12-14-2010
Code:
sed 's/\([[:graph:]]*=\)/\n\1/g' infile

# 7  
Old 12-14-2010
Code:
sed 's/[^= \t]*=/\n&/g' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

2. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

3. Programming

read() contents from a file

Hi, I'm trying to implement a C program on ubuntu which reads the contents of a file that is passed in as an argument and then displays it to the screen. So far I've cobbled together this from bits online but most of it is probably wrong as its all copied and pasted... #include <stdio.h>... (2 Replies)
Discussion started by: cylus99
2 Replies

4. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (1 Reply)
Discussion started by: erlanq
1 Replies

5. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (2 Replies)
Discussion started by: erlanq
2 Replies

6. Shell Programming and Scripting

how to read contents of file?

I have made a script something like this. I want it to read the contents of either file or directory but 'cat' and 'ls' is not working. Can anyone help me? I am a newbie in scripting so dont know much about it. I also dont know how can i put my code separatly on this forum #!/bin/bash echo... (9 Replies)
Discussion started by: nishrestha
9 Replies

7. Shell Programming and Scripting

Read contents from a file

Hi Friends, I am new to this forum. Just struck up with a logic. I have a csv file seperated by ":" (colons). This csv file contains hostname and groups as follows: HOSTNAME:VT Group SGSGCT2AVPX001:Team1 SGSGCT2AVPX003:Team2 SGSGCT2AVPX005:Team2 PHMNCTTAVPX001:Team3 I want to... (2 Replies)
Discussion started by: dbashyam
2 Replies

8. Shell Programming and Scripting

Read 1-line file and separate into multiple variables

I have one line files with 17 records separated by a semi-colon. I need to create a variable from each record, which I can do via a separate awk for each one, but I know there has to be a better way. Along with pulling out the variable, I need to convert some url coding like a + to a space, etc.... (4 Replies)
Discussion started by: numele
4 Replies

9. Shell Programming and Scripting

read contents of a file with serveral lines & assign it to a variable

Hi All I have a file for ex .log file which contain several lines within it. I have to read that file contents & assing that to a variable. (2 Replies)
Discussion started by: satyam.sumit
2 Replies

10. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies
Login or Register to Ask a Question