Need to format string to collate data of similar IP address.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to format string to collate data of similar IP address.
# 1  
Old 01-02-2020
Need to format string to collate data of similar IP address.

I have a string which has IP and data of files for that ip which is printed using the below code: Sample string code below:

Code:
str1="10.9.11.128\n-rwxr-xr-x user1 2019-12-29 17:53 /var/branch/custom/tg.xml 286030210\n10.9.12.129\n-rwxr-xr-x user1 2019-12-29 17:53 /app/branch/custom/tg.xml 286030210\n10.9.20.130\n-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/tg.xml 286030210\n10.9.11.128\n-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/mg.xml 286030210\n-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/nog.xml 999997"

    while read -r fline; do
       echo "$fline" | sed 's/\\n/\n/g'
    done <<< "$str1"

Below is the current output:
Code:
./test.sh 
10.9.11.128
-rwxr-xr-x user1 2019-12-29 17:53 /var/branch/custom/tg.xml 286030210
10.9.12.129
-rwxr-xr-x user1 2019-12-29 17:53 /app/branch/custom/tg.xml 286030210
10.9.20.130
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/tg.xml 286030210
10.9.11.128
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/mg.xml 286030210
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/nog.xml 999997

My requirement is to club file details for similar IPs into one.
Thus my desired output is as below:
Code:
10.9.11.128
-rwxr-xr-x user1 2019-12-29 17:53 /var/branch/custom/tg.xml 286030210
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/mg.xml 286030210
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/nog.xml 999997
10.9.12.129
-rwxr-xr-x user1 2019-12-29 17:53 /app/branch/custom/tg.xml 286030210
10.9.20.130
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/tg.xml 286030210

As you can see file details of "10.9.11.128" are clubbed as a single entry so the IP is not repeated.

This is just a sample ... I may have any number of IPs and any number of files under them as a string.

Can you please suggest how we can achieve this output ?
# 2  
Old 01-02-2020
Quote:
Originally Posted by mohtashims
Code:
...
    while read -r fline; do
       echo "$fline" | sed 's/\\n/\n/g'
    done <<< "$str1"

What do you think how such a design works? Just add a counter to make it clearer
Code:
while read -r fline; do
       ((counter++))
       echo "$fline" | sed 's/\\n/\n/g'
       echo $counter
done <<< "$str1"

--- Post updated at 11:09 ---

Now so
Code:
while read fline; do
        ((count++))
        echo "$fline"
        echo $count
done < <(echo -e $str1)

# 3  
Old 01-02-2020
@nezabudka here is the output:

For:
Code:
while read -r fline; do
       ((counter++))
       echo "$fline" | sed 's/\\n/\n/g'
       echo $counter
done <<< "$str1"

Output:
Code:
./test2.sh 
10.9.11.128
-rwxr-xr-x user1 2019-12-29 17:53 /var/branch/custom/tg.xml 286030210
10.9.12.129
-rwxr-xr-x user1 2019-12-29 17:53 /app/branch/custom/tg.xml 286030210
10.9.20.130
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/tg.xml 286030210
10.9.11.128
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/mg.xml 286030210
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/nog.xml 999997
1

For:
Code:
while read fline; do
        ((count++))
        echo "$fline"
        echo $count
done < <(echo -e $str1)

Output:
Code:
./test1.sh 
10.9.11.128
1
-rwxr-xr-x user1 2019-12-29 17:53 /var/branch/custom/tg.xml 286030210
2
10.9.12.129
3
-rwxr-xr-x user1 2019-12-29 17:53 /app/branch/custom/tg.xml 286030210
4
10.9.20.130
5
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/tg.xml 286030210
6
10.9.11.128
7
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/mg.xml 286030210
8
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/nog.xml 999997
9

# 4  
Old 01-02-2020
Code:
awk '
/^[0-9.]+$/     {st=$0; next}; {T[st] = $0 RS T[st]}
END             {for(i in T) printf "%s", i RS T[i]}
' < <(echo -e $str1)

Then replace this expression "< <(echo -e $str)" with a real file
# 5  
Old 01-02-2020
Quote:
Originally Posted by nezabudka
Code:
awk '
/^[0-9.]+$/     {st=$0; next}; {T[st] = $0 RS T[st]}
END             {for(i in T) printf "%s", i RS T[i]}
' < <(echo -e $str1)

Then replace this expression "< <(echo -e $str)" with a real file
I'm getting syntax error when i specify a file.
Code:
awk '
/^[0-9.]+$/     {st=$0; next}; {T[st] = $0 RS T[st]}
END             {for(i in T) printf "%s", i RS T[i]}
' < < (cat report1.txt) >>report.txt
line 303: syntax error near unexpected token `<'

# 6  
Old 01-02-2020
Code:
awk '
/^[0-9.]+$/     {st=$0; next}; {T[st] = $0 RS T[st]}
END             {for(i in T) printf "%s", i RS T[i]}
' report1.txt >report.txt

--- Post updated at 14:16 ---

Code:
' < < (cat report1.txt) >>report.txt

there should be no space before parentheses
Code:
' <(cat report1.txt) >>report.txt

And the first redirection icon "<" is optional

Last edited by nezabudka; 01-02-2020 at 06:25 AM..
# 7  
Old 01-03-2020
Command:
Code:
perl -ne '/^\d/ and $c=$_ and next; $ip{$c} .= $_; END{for $i (sort keys %ip){print "$i$ip{$i}"}}' report1.txt

Output:
Code:
10.9.11.128
-rwxr-xr-x user1 2019-12-29 17:53 /var/branch/custom/tg.xml 286030210
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/mg.xml 286030210
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/nog.xml 999997
10.9.12.129
-rwxr-xr-x user1 2019-12-29 17:53 /app/branch/custom/tg.xml 286030210
10.9.20.130
-rwxr-xr-x user1 2019-12-29 17:53 /web/branch/custom/tg.xml 286030210

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to group similar data?

Hello everybody: I want rearrange about 5 million rows (with 300 columns) into groups. Data looks like the following: where there were various experiments (column 2) conducted at different locations (column headers in top row column 4 onwards) in different years (column 1) using instruments... (0 Replies)
Discussion started by: sheetalk
0 Replies

2. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

Kindly check:remove duplicates with similar data in front of it

Hi all, I have 2 files containing data like this: so if there is same entry repeated in the column like1,2,3,4 I have to check if there is different entries column like 2,4 but similar entries for duplicatein column 2 like1,3 the output shuld be like this for first file ... (5 Replies)
Discussion started by: manigrover
5 Replies

5. Shell Programming and Scripting

capturing the value in file before string(*) and the similar value in next line only

I've the output in the file like below. I want to capture the value in file before string(*) and the similar value in next line only. cat test1.txt 0003 Not Visible (M) 0 00 03F 0005 Not Visible (M) 0 00 040 - AVAILABLE 0 00... (1 Reply)
Discussion started by: sai_1712
1 Replies

6. Shell Programming and Scripting

appending data from similar files

I am familiar with scripting, but I am trying to see if there is an easy way to append files from similar files into one file. For example, if there is file1_20121201, file1_20121202, file1_20121203, file2_20121201, file2_20121202, file2_20121203 I want to be able to combine all the data from... (3 Replies)
Discussion started by: mrbean1975
3 Replies

7. Shell Programming and Scripting

IP address to decimal format conversion

I have a file which consist of some class 4 IP address as 172.16.112.50 172.16.112.50 172.16.112.50 172.16.112.100 192.168.1.30 172.16.112.100 172.16.112.50 172.16.112.50 172.16.112.50 i want to store them in pure decimal notations instead of the given dotted decimal formats e.g.... (2 Replies)
Discussion started by: vaibhavkorde
2 Replies

8. Shell Programming and Scripting

Searching String from set of similar File pattern from the Dir

Guys, Here is the script that searches string from the set of similar files from the log directory, All the file patterns are defined as input file, from where the script should map to those files in the LOG_DIR and should start searching the strings from all those similar files. ... (1 Reply)
Discussion started by: raghunsi
1 Replies

9. Shell Programming and Scripting

bash: convert mac address to 16 character format

Hi there Im not quite sure how i can do this, but i am retrieving the mac address from boxes, which in some instances is arriving in its shortened format (i.e. dropping the leading zeros)... for example 0:3:BA:1:E:84 Im trying to figure out a way of converting the single character... (3 Replies)
Discussion started by: rethink
3 Replies

10. UNIX for Dummies Questions & Answers

converting a tabular format data to comma seperated data in KSH

Hi, Could anyone help me in changing a tabular format output to comma seperated file pls in K-sh. Its very urgent. E.g : username empid ------------------------ sri 123 to username,empid sri,123 Thanks, Hema:confused: (2 Replies)
Discussion started by: Hemamalini
2 Replies
Login or Register to Ask a Question