0

Need help to display increment odd & even lines in a text file to screen.

Textfile

10101 
AsPO4IF+tGU=
01010 
WvTzUYrxvXY=
00101
CfZzsDDKW98=

Stuck at the following script.

result=Textfile
while IFS='' read -r line || [[ -n "$line" ]];
echo "`awk or sed odd line $result`" "`awk or sed even line $result`"
done < "$result"

Output needed

"AsPO4IF+tGU=" "10101" 

"WvTzUYrxvXY=" "01010" 

"CfZzsDDKW98=" "00101" 

Stops script once EOF in Textfile has reached.

Thanks for your time.

  • 1
    Is there a question in here? – ilkkachu Jul 15 '19 at 13:26
  • Are the trailing whitespaces after 10101 and 01010 intended? – Philippos Jul 15 '19 at 13:57
  • Thanks all for your amazing responses...the answers was what I wanted. – Troy Aug 18 '19 at 22:20
1

You don't need to loop lines of a file, awk is expert in exactly that:

awk '
    NR%2 != 0 {last=$0}
    NR%2 == 0 {printf "\"%s\" \"%s\"\n",$0,last}
' file
1

With sed you don't need to have an external loop either:

sed -E '$!N;s/(.*)\n(.*)/"\2" "\1"/' file
  • -E is to use extended regular expression, which makes the ()easier to read
  • $!N always joins (N) two lines in the pattern space except for the last line ($!), if there should be an unpaired line
  • s/(.*)\n(.*)/"\2" "\1"/ flips both lines, replaces the newline with a whitespace and adds the double quotes
0

I have done by below method

command:

sed "N;s/\n/ /g" filename | awk '{print $2,$1}'| sed 's/^/"/g'| sed 's/$/"/g'| sed 's/=/"&"/g'

output

"AsPO4IF+tGU"=" 10101"
"WvTzUYrxvXY"=" 01010"
"CfZzsDDKW98"=" 00101"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.