Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 03-01-2011, 05:47 AM   #1
LQ Newbie
Registered: Mar 2011
Posts: 2

Rep: Reputation: 0
concatenate data from different lines

Hi everybody.

I want to concatenate info from different lines from iptables. for exmaple, in my firewall using iptables I have this information.

pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * *
5 248 ACCEPT all -- * *

This shows how many bytes a user with ip has down- and uploaded. I now want to print out a line that looks like this.

"IP, downloaded bytes, uploaded bytes"

But since informations are split over two lines i don't know how to combine them.

In a real world example there will of course be many more lines.

Hope someone can help.

Old 03-01-2011, 08:15 AM   #2
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Hello kiloman,

This feels like homework, so I'll just point you in the general direction

There are several ways to do this.
You could do it all in Perl, or you could do it just from a command prompt.

If you went down the command prompt route, you can join every other line using 'sed':
$ /your/iptables/command --andopts | sed '$!N;s/\n/ /'
You could then pipe this output through 'awk' and just output the fields you want:
$ /your/iptables/command --andopts | sed '$!N;s/\n/ /' | awk 'BEGIN { print "IP downloaded-bytes uploaded-bytes" } { print $1 " " $2 " " $3 }'
(the positional parameters used above ($1, $2 and $3) would need setting to whatever field you required, based on the output of the first command)

This code presumes that your iptables command only shows you the data, and there are no column names or other identifiers in the mix. I don't use iptables, so I'm not familiar with the layout.

Good luck!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Extract multiple lines of data from a text file. shawnamiller Programming 8 04-30-2010 12:46 PM
extracting lines from very large data files lothario Linux - Software 1 12-15-2009 10:22 PM
have a Variable with multiple lines of data need to have it all on one line seperated xskycamefalling Programming 11 05-12-2009 05:44 AM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 11:43 AM
use file data in command lines karlis Linux - General 3 08-29-2003 08:27 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:17 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration