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:
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:
./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:
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 ?
What do you think how such a design works? Just add a counter to make it clearer
while read -r fline; do
((counter++))
echo "$fline" | sed 's/\\n/\n/g'
echo $counter
done <<< "$str1"
--- Post updated at 11:09 ---
Now so
while read fline; do
((count++))
echo "$fline"
echo $count
done < <(echo -e $str1)
@nezabudka here is the output:
For:
while read -r fline; do
((counter++))
echo "$fline" | sed 's/\\n/\n/g'
echo $counter
done <<< "$str1"
Output:
./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:
while read fline; do
((count++))
echo "$fline"
echo $count
done < <(echo -e $str1)
Output:
./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
awk '
/^[0-9.]+$/ {st=$0; next}; {T[st] = $0 RS T[st]}
END {for(i in T) printf "%s", i RS T}
' < <(echo -e $str1)
Then replace this expression "< <(echo -e $str)" with a real file
I'm getting syntax error when i specify a file.
awk '
/^[0-9.]+$/ {st=$0; next}; {T[st] = $0 RS T[st]}
END {for(i in T) printf "%s", i RS T}
' < < (cat report1.txt) >>report.txt
line 303: syntax error near unexpected token `<'
awk '
/^[0-9.]+$/ {st=$0; next}; {T[st] = $0 RS T[st]}
END {for(i in T) printf "%s", i RS T}
' report1.txt >report.txt
--- Post updated at 14:16 ---
' < < (cat report1.txt) >>report.txt
there should be no space before parentheses
' <(cat report1.txt) >>report.txt
And the first redirection icon "<" is optional
Aia
7
Command:
perl -ne '/^\d/ and $c=$_ and next; $ip{$c} .= $_; END{for $i (sort keys %ip){print "$i$ip{$i}"}}' report1.txt
Output:
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