Hi
I need to order these lines from a txt file my file looks like this
IMSI ........................ 1234567890
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM
IMSI ........................ 0123456789
IMSI ........................ 1234567890
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM
Is there any way that my file looks like that
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
IMSI 0123456789
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
Thanks
Hello Alone77,
Welcome to forum, following may help you in same.
awk '{gsub(/\./,X,$0);} {for(i=1;i<=NF;i++){if($i == "IMSI") {print "\n"$i} else {print $i}}}' ORS=" " Input_file
Output is as follows.
IMSI 1234567890 APN INTERNETCOM APN MMSCOM APN WAPCOM APN BACOM
IMSI 0123456789
IMSI 1234567890 APN INTERNETCOM APN MMSCOM APN WAPCOM APN BACOM
EDIT: Above solution will give a blank line in starting if word IMSI
is present in first line, so following will remove this loop whole.
awk '{gsub(/\./,X,$0);} {for(i=1;i<=NF;i++){if($i == "IMSI" && NR != 1) {print "\n"$i} else {print $i}}}' ORS=" "
EDIT: One more solution on same.
awk '{gsub(/\./,X,$0);} {for(i=1;i<=NF;i++){$i=$i == "IMSI" && NR != 1?$i="\n"$i:$i;print $i}} END{ORS="";print "\n"}' ORS=" " Input_file
Output will be as follows.
IMSI 1234567890 APN INTERNETCOM APN MMSCOM APN WAPCOM APN BACOM
IMSI 0123456789
IMSI 1234567890 APN INTERNETCOM APN MMSCOM APN WAPCOM APN BACOM
Thanks,
R. Singh
[akshay@nio tmp]$ cat file
IMSI ........................ 1234567890
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM
IMSI ........................ 0123456789
IMSI ........................ 1234567890
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM
[akshay@nio tmp]$ awk 's=/IMSI/{printf("%s",(++i==1?"":RS))}{printf("%s%s%s%s",(s?"":OFS),$1,OFS,$3)}END{print ""}' file
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
IMSI 0123456789
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
---------- Post updated at 02:33 PM ---------- Previous update was at 02:22 PM ----------
OR like this
[akshay@nio tmp]$ awk 'function p(){if(length(s)){print s;s=""}}{if(/IMSI/)p();s = (length(s) ? s OFS : "") $1 OFS $NF;}END{p()}' file
RudiC
5
Try
awk '/IMSI/ && NR>1 {printf "\n"}; END {printf "\n"} {printf "%s %s ",$1, $NF}' ORS=" " file
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
IMSI 0123456789
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
Perl if interested
perl -lane 'sub p{print "@_" if @_ }; p(@S) and undef @S if eof or /IMSI/; push(@S,@F[0,$#F]);' file
OR bit lengthy
perl -lane 'printf("%s",(++$i==1?"":"\n")) if $s=/IMSI/; printf("%s%s%s%s",($s?"":" "),$F[0]," ",$F[$#F]); print "" if eof' file