Aligning a file

I have a large text file in following format

cat input.txt

abc
qwert
qwer
afweferf
wdfwefwe ==> kjhjkwdd
mnmn ==> jkjkjwekj
poiu ==> lklklke
tytyutut ==> olkjmnsmn

I need to align those lines with the characters " ==>" . I dont want to disturb the lines which dont have "==>".

The output needs to be as below:

cat output.txt

abc
qwert
qwer
afweferf
wdfwefwe ==> kjhjkwdd
mnmn     ==> jkjkjwekj
poiu     ==> lklklke
tytyutut ==> olkjmnsmn

It must read the file twice, first to determine the maximum width, the second to align:

$ awk -F"==>" -v OFS="==>" 'NR==FNR { if(length($1) > L) L=length($1) ; next } NF>1 { $1=sprintf("%-"L"s",$1) } 1' input.txt input.txt
abc
qwert
qwer
afweferf
wdfwefwe  ==> kjhjkwdd
mnmn      ==> jkjkjwekj
poiu      ==> lklklke
tytyutut ===> olkjmnsmn

$

There is an extra = in the output because there's an extra = in the input.

1 Like

Or try using column command:

$ column -t input.txt
abc
qwert
qwer
afweferf
wdfwefwe  ==>   kjhjkwdd
mnmn      ==>   jkjkjwekj
poiu      ==>   lklklke
tytyutut  ===>  olkjmnsmn
2 Likes

Thanks Corona and Yoda.

I had already tried column command and it is aligning the lines without "==>" also.
For example, if I have a line like below in my input.txt the output goes wrong.

cat input.txt

abc
qwert
qwer
afweferf
wdfwefwe ==> kjhjkwdd
mnmn ==> jkjkjwekj
poiu ==> lklklke
tytyutut ==> olkjmnsmn
This text need not be aligned.

column -t input.txt

abc
qwert
qwer
afweferf
wdfwefwe  ==>   kjhjkwdd
mnmn      ==>   jkjkjwekj
poiu      ==>   lklklke
tytyutut  ==>   olkjmnsmn
This      text  need       not  be  aligned.

If I try column -s "==>" -t input.txt:

 
column -s "==>" -t input.txt
abc
qwert
qwer
afweferf
wdfwefwe                         kjhjkwdd
mnmn                             jkjkjwekj
poiu                             lklklke
tytyutut                         olkjmnsmn
This text need not be aligned.

The ==> is getting removed.

---------- Post updated at 08:06 AM ---------- Previous update was at 07:30 AM ----------

I just noticed one thing, the text before "==>" does not exceed 50 characters in max length. I think placing the ==> at exactly 50th character position should do the trick.

Try :

$ awk 'NR==FNR{ if(/\==>/){ l =  length($1) > l ? length($1) : l } next }/\==>/{$1=sprintf("%-"l"s",$1)}1' file file
$ awk '/\==>/{$1=sprintf("%-"l"s",$1)}1' l=48 file
1 Like

Yes it worked like a charm.. :b:

Thanks Akshay.

Hi.

The general alignment program align provides a feature for processing only lines on which patterns match:

#!/usr/bin/env bash

# @(#) s1	Demonstrate align text for matched lines, align.
# For align code, see:
# http://freecode.com/projects/align

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C align

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, align text only in lines in which \"==>\" appears:"
align -e "/==>/" $FILE

pl " Results, with a wider gutter:"
align -g 5 -e "/==>/" $FILE

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
align 1.7.0

-----
 Input data file data1:
abc
qwert
qwer
afweferf
wdfwefwe ==> kjhjkwdd
mnmn ==> jkjkjwekj
poiu ==> lklklke
tytyutut ==> olkjmnsmn
This text need not be aligned.

-----
 Results, align text only in lines in which "==>" appears:
abc
qwert
qwer
afweferf
wdfwefwe ==> kjhjkwdd
mnmn     ==> jkjkjwekj
poiu     ==> lklklke
tytyutut ==> olkjmnsmn
This text need not be aligned.

-----
 Results, with a wider gutter:
abc
qwert
qwer
afweferf
wdfwefwe     ==>     kjhjkwdd
mnmn         ==>     jkjkjwekj
poiu         ==>     lklklke
tytyutut     ==>     olkjmnsmn
This text need not be aligned.

The code for align can be obtained from the URL noted in the first part of the script.

Best wishes ... cheers, drl

2 Likes