Parsing a file in bash


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Parsing a file in bash
# 1  
Old 07-25-2014
Parsing a file in bash

Hello All,
I have the following input file that i'm trying to parse:
HTML Code:
10.0.011.40
hadoop 15526 15524 0
hadoop 15528 15526 0
hadoop 19747 4018 1
10.0.081.227
hadoop 2862 2861 0
hadoop 2864 2862 0
hadoop 12177 14376 1
I'm trying to get this in my output file:
HTML Code:
10.0.011.40 15526 15528 19747
10.0.081.227 2862 2864 12177
I have tried this code:
HTML Code:
#!/usr/bin/bash
#set -x
while read line
do
  if [[ $line =~ ^[0-9] ]];then
    IPADDR=`echo "$line" | awk --posix '{if ($1 ~ /^[0-9]/) print $1}'`
  else
    PID=`echo "$line" | awk '{ print $2 }'`
  fi
  IPADDR+=($PID)
  echo ${IPADDR[@]} > ex2
done < ex1
The problem;
I have more than 1 PID rows ( hadoop rows, for each server)

Last edited by ramky79; 07-25-2014 at 04:08 PM..
# 2  
Old 07-25-2014
You could do it using awk alone:
Code:
awk '
        /^[0-9]/ {
                ip = $1
                next
        }
        !/^[0-9]/ {
                A[ip] = A[ip] ? A[ip] FS $2 : $2
        }
        END {
                for ( k in A )
                        print k, A[k]
        }
' ex1

# 3  
Old 07-26-2014
Try :

if you have gawk, if pattern is ip address...

Code:
awk --re-interval '
/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/{
	if(p)print ""
	printf("%s%s",$0,OFS)
	next
}
{
	printf $2 OFS
	p=1
}
END{
	print ""
}'

or just this if assumption is numeric char

Code:
awk  '
/^[0-9]/{
	if(p)print ""
	printf("%s%s",$0,OFS)
	next
}
{
	printf $2 OFS
	p=1
}
END{
	print ""
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing to_addr field in bash

Hi there, I'm trying to parse the to_addr field of emails and split it into individual email addresses. The idea is to split using the comma character (,): These two first approach work: $ field="'Paul FOO' <paul@foo.com>, Andrew FOO <andrew@foo.com>" $ IFS=, read -r -a array <<< "$field";... (4 Replies)
Discussion started by: chebarbudo
4 Replies

2. Shell Programming and Scripting

Parsing and Editing a json file with bash script

I am trying to automate editing of a json file using bash script. The file I initially receive is { "appMap": { "URL1": { "name": "a" }, "URL2": { "name": "b" }, "URL3": { "name": "c" }, } WHat I would like to do is replace... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

3. Shell Programming and Scripting

Bash script not parsing file with spaces in path

Hi everyone, I'm trying to write my first ever shell script, the OS is Raspbian. The code I have written must be executed whenever a certain database has been modified. The database resides on a Windows server to which I have a mount point, and I have no control over the Windows server at all so... (2 Replies)
Discussion started by: gjws
2 Replies

4. Shell Programming and Scripting

Column parsing in a bash script - HELP

I would like to setup a script that pulls in time/date in two seperate columns, and then name the other columns as listed below: Column1=Art/TJ output Column2=Art/TJ output Column3=TJ output column4=Art output Column5=If time/date past 12:00 noon -fail Colume6=If time/date before... (1 Reply)
Discussion started by: walnutpony123
1 Replies

5. Shell Programming and Scripting

BASH parsing for html tags

Hello can anyone help me parse this line. <tr><td>United States of America</td><td>Dollar</td><td>43.309</td></tr><tr><td>Japan</td><td>Yen</td><td>0.5579</td></tr> the line above did not break. so i would like to have a result like this United States of America Dollar 43.309 Japan... (3 Replies)
Discussion started by: doomsayer16
3 Replies

6. Shell Programming and Scripting

Looking for help with parsing file contents in bash [newbie]

Hi I'm just messing around with bash and trying to learn it because I have a course next semester dealing with OS design where we need to know how to use SSH client and either bash or ksh. I've never done shell scripting before. I just started today and I was wondering how parsing files... (1 Reply)
Discussion started by: mehungry
1 Replies

7. Shell Programming and Scripting

parsing a config file using bash

Hi , I have a config _file that has 3 columns (Id Name Value ) with many rows . In my bash script i want to be able to parse the file and do a mapping of any Id value so if i have Id of say brand1 then i can use the name (server5X) and Value (CCCC) and so on ... Id Name ... (2 Replies)
Discussion started by: nano2
2 Replies

8. Shell Programming and Scripting

Parsing (Bash)

Hello, I need help. I create www page, and I have link, where is weather and is updated each hour. And I need cut only weather from source code. Example: Monday : 12/14 ... Can you help me? Thanks (2 Replies)
Discussion started by: krcek12
2 Replies

9. Shell Programming and Scripting

Help parsing filename with bash script

Hi all! Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to... (4 Replies)
Discussion started by: mtehonica
4 Replies

10. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies
Login or Register to Ask a Question