Please help me in organizing the data in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help me in organizing the data in UNIX
# 1  
Old 07-13-2015
Please help me in organizing the data in UNIX

I have an output file in the form

Code:
Hostname
Value1=abc
Value2=def
Value3=xyz
Hostname1
Value1=abc1
Value2=def1
Value3=xyz1
Hostname2
Value1=abc2
Value2=def2
Value3=xyz2
|
|
|

And so on…..

I need to export this output into csv so then it should be in format

Code:
Hostname          value1                  value2                  value3
Hostname          abc                         def                         xyz
Hostname1        abc1                      def1                      xyz1
Hostname2        abc2                      def2                      xyz2

Please help me in achieving this. Thanks a lot.

Last edited by rbatte1; 07-13-2015 at 09:47 AM.. Reason: Added
# 2  
Old 07-13-2015
Please use code tags as required by forum rules!

Any attempt from your side?

---------- Post updated at 10:14 ---------- Previous update was at 10:03 ----------

Howsoever, if the file structure is exactly as posted (four lines per host, in sequence), try
Code:
awk '
BEGIN   {print "Hostname value1 value2 value3"}
        {sub (/^.*=/,_)
         printf "%s%s", $1, NR%4?" ":"\n" 
        }
' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-13-2015
Thanks a lot RudiC for your script. I was previously getting error from my script and now my script is working fine. Below is my script :
Code:
#!/bin/bash
echo "hostname value1 value2 value3" > result.csv
a=`cat $1 | wc -l`
echo $a
b=`expr ${a} / 4`
echo $b
i=1
while [ $i -le $b ]
do[/FONT]
echo "Inside loop"
echo $i
c=`expr ${i} \* 4`
echo "c=$c"
cat $1 | head -n $c | tail -n 4 > t2.csv

Moderator's Comments:
Mod Comment
Please do not paste masses of FONT formatting, just wrap all code, files, input & output/errors in CODE tags.
It makes them far easier to read and retains multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 07-13-2015 at 10:55 AM.. Reason: Added CODE tags
# 4  
Old 07-15-2015
Hello RudiC , Can you please explain me the script. I had earlier used awk to print the columns but i never used functions inside the awk statement. Thanks a lot for the efforts.
# 5  
Old 07-15-2015
Hello Rahul,

Following is the explanation for same, hope this may help you.
Code:
 awk '
BEGIN   {print "Hostname value1 value2 value3"}            #### In starting of any operation print Hostname value1 value2 value3 as headers as per your request
        {sub (/^.*=/,_)                                    #### Substitute any line which has match from starting to till = with NULL
         printf "%s%s", $1, NR%4?" ":"\n"                  #### then print the values of $1 as first string and as second string print space " " when NR%4 is having some values and if NR%4 has no values then print new line.
        }                                                       Means we are only printing new line after 4th line. operators ? means when a condition is TRUE then follow the instructions after that operator and instructions proceeding : operator are being
'                                                               done when condition is FALSE.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 07-15-2015
OR
Code:
awk -F'=' 'BEGIN{print "Hostname value1 value2 value3"}{printf("%s%s",$NF,NR%4?" ":RS)}' file

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 07-16-2015
Hello Ravinder , I have one question from the below function :
sub (/^.*=/,_)

why we have used ".*" and why not * ?
does _ signifies null substitution in the above function ?

Thanks a lot for your efforts.

Rahul
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

2. Shell Programming and Scripting

help with organizing some non regular text

hey im trying to get the hex diffrences in two files ones called new and the other is named old i want it to phrase into my script, heres how i need the info: input='\x'94 #the new 1 byte hex change offset=00000000 #the 1st offset of the difference patch unset input offset input='\x'34... (5 Replies)
Discussion started by: lewisdenny
5 Replies

3. Programming

Organizing C++ Code

I have three directories CspInterp, FpnInterp and LinInterp. Each directory contains 4 .h and .ccp files describing 4 classes each CspInterp class CspFsInterp1d : public FsInterp1d class CspVsInterp1d : public VsInterp1d class CspFsInterp2d : public FsInterp2d class CspVsInterp2d : public... (10 Replies)
Discussion started by: kristinu
10 Replies

4. Shell Programming and Scripting

Organizing log

Hello, Following are the log of my sms application COMMAND: #tail -30 /var/log/smsd.log | grep Message_id | awk '{print $1,$2,$9}' OUTPUT: 2011-02-21 12:16:20,5, 03218975857, 2011-02-21 12:16:26,5, 03323048252, 2011-02-21 12:16:53,5, 03323048252, 2011-02-21 12:16:59,5,... (1 Reply)
Discussion started by: telnor
1 Replies

5. Shell Programming and Scripting

Organizing Files

Hi, I'm fairly new at scripting. I need to write a script that takes files from a source directory puts them in a target directory and sorts them by artist name. This is what I have so far #!/bin/bash source_dir='/home/tcindy/songs' target_dir='/home/tcindy/music' for path in... (2 Replies)
Discussion started by: tcindy
2 Replies

6. Shell Programming and Scripting

Organizing a log

I have a bunch of log files generated from a shell script, its all of my facebook friends and if theyre logged in. Each file is a different person. It runs every 5 minutes. The log file is just the date and time, then 1 if theyre logged in or 0 if theyre not. part of one of the files is: Mon Aug... (5 Replies)
Discussion started by: killer54291
5 Replies

7. Shell Programming and Scripting

Help organizing a music directory with sub directories.

Hello all, I used to have a great script and I lost it :( What the script did was searched a directory named say "music" It searched all sub directories for .mp3 files Then placeed all the .mp3's into a directory of the band name It also renamed the .mps "track#, band name, album name" (I... (9 Replies)
Discussion started by: komputersman
9 Replies

8. UNIX for Dummies Questions & Answers

Validating XSL sheet data in Unix Data file

Dear All, Need your help. In my day to day activities I have to validate/search Excel Sheet data (eg.say Application No. 0066782345) data into the Unix environment file whether the same data is present in that file or not. There are hundreds of records coming in excel file and I am doing grep... (1 Reply)
Discussion started by: ravijunghare
1 Replies

9. UNIX for Advanced & Expert Users

how to read the data from an excel sheet and use those data as variable in the unix c

I have 3 columns in an excel sheet. c1 c2 c3 EIP_ACCOUNT SMALL_TS_01 select A.* from acc; All the above 3 col shoud be passed a variable in the unix code. 1.How to read an excel file 2.How to pass these data as variable to the unic script (1 Reply)
Discussion started by: Anne Grace
1 Replies
Login or Register to Ask a Question