Problem in formatting output in sed / awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in formatting output in sed / awk
# 8  
Old 04-08-2017
Quote:
Originally Posted by Scrutinizer
Small correction:
Code:
awk '
  BEGIN {
    FS="!"; OFS="!"
  }
  {
    for (i=1;i<=NF;i++) {
      l=length($i)
      gsub(/,/,x,$i)
      $i=sprintf("%*s",l,$i)
    }
    print
  }
' file

(the "1" was removed)
Thank you very much sir.
And thanks to all. Smilie
# 9  
Old 04-08-2017
Hi.

Using the free utility align:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate automatic field alignment, 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 "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C align dixf

FILE=${1-data1}

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

pl " Results, default:"
align $FILE

pl " Results, wider gutter:"
align -g 3 $FILE

pl " More details for align:"
dixf align

exit 0

producing:
Code:
$ ./s1 

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
align 1.7.0
dixf (local) 1.42

-----
 Input data file data1:
!    1 ! 542255 !    50140.00  !
!    2 ! 551717 !     5805.00  !
!    3 ! 551763 !     8130.00  !
!    4 ! 551779 !       750.00  !
!    5 ! 551810 !    56580.00  !
!    6 ! 551816 !     1350.00  !
!    7 ! 551876 !       360.00  !
!    8 ! 551898 !     6580.00  !
!    9 ! 557285 !    69295.00  !
!   10 ! 557508 !     6685.00  !

-----
 Results, default:
!  1 ! 542255 ! 50140.00 !
!  2 ! 551717 !  5805.00 !
!  3 ! 551763 !  8130.00 !
!  4 ! 551779 !   750.00 !
!  5 ! 551810 ! 56580.00 !
!  6 ! 551816 !  1350.00 !
!  7 ! 551876 !   360.00 !
!  8 ! 551898 !  6580.00 !
!  9 ! 557285 ! 69295.00 !
! 10 ! 557508 !  6685.00 !

-----
 Results, wider gutter:
!    1   !   542255   !   50140.00   !
!    2   !   551717   !    5805.00   !
!    3   !   551763   !    8130.00   !
!    4   !   551779   !     750.00   !
!    5   !   551810   !   56580.00   !
!    6   !   551816   !    1350.00   !
!    7   !   551876   !     360.00   !
!    8   !   551898   !    6580.00   !
!    9   !   557285   !   69295.00   !
!   10   !   557508   !    6685.00   !

-----
 More details for align:
align   Align columns of text. (what)
Path    : ~/p/stm/common/scripts/align
Version : 1.7.0
Length  : 270 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Help    : probably available with --help
Home    : http://kinzler.com/me/align/
Modules : (for perl codes)
 Getopt::Std    1.10

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 10  
Old 04-08-2017
With sed and a loop
Code:
sed '
:L
s/\([0-9]\{1,\}\),/ \1/
tL
' file

This User Gave Thanks to MadeInGermany For This Post:
# 11  
Old 04-08-2017
Quote:
Originally Posted by drl
Hi.

Using the free utility align:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate automatic field alignment, 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 "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C align dixf

FILE=${1-data1}

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

pl " Results, default:"
align $FILE

pl " Results, wider gutter:"
align -g 3 $FILE

pl " More details for align:"
dixf align

exit 0

producing:
Code:
$ ./s1 

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
align 1.7.0
dixf (local) 1.42

-----
 Input data file data1:
!    1 ! 542255 !    50140.00  !
!    2 ! 551717 !     5805.00  !
!    3 ! 551763 !     8130.00  !
!    4 ! 551779 !       750.00  !
!    5 ! 551810 !    56580.00  !
!    6 ! 551816 !     1350.00  !
!    7 ! 551876 !       360.00  !
!    8 ! 551898 !     6580.00  !
!    9 ! 557285 !    69295.00  !
!   10 ! 557508 !     6685.00  !

-----
 Results, default:
!  1 ! 542255 ! 50140.00 !
!  2 ! 551717 !  5805.00 !
!  3 ! 551763 !  8130.00 !
!  4 ! 551779 !   750.00 !
!  5 ! 551810 ! 56580.00 !
!  6 ! 551816 !  1350.00 !
!  7 ! 551876 !   360.00 !
!  8 ! 551898 !  6580.00 !
!  9 ! 557285 ! 69295.00 !
! 10 ! 557508 !  6685.00 !

-----
 Results, wider gutter:
!    1   !   542255   !   50140.00   !
!    2   !   551717   !    5805.00   !
!    3   !   551763   !    8130.00   !
!    4   !   551779   !     750.00   !
!    5   !   551810   !   56580.00   !
!    6   !   551816   !    1350.00   !
!    7   !   551876   !     360.00   !
!    8   !   551898   !    6580.00   !
!    9   !   557285   !   69295.00   !
!   10   !   557508   !    6685.00   !

-----
 More details for align:
align   Align columns of text. (what)
Path    : ~/p/stm/common/scripts/align
Version : 1.7.0
Length  : 270 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Help    : probably available with --help
Home    : http://kinzler.com/me/align/
Modules : (for perl codes)
 Getopt::Std    1.10

Best wishes ... cheers, drl
Thank you sirSmilie

---------- Post updated at 08:17 AM ---------- Previous update was at 08:16 AM ----------

Quote:
Originally Posted by MadeInGermany
With sed and a loop
Code:
sed '
:L
s/\([0-9]\{1,\}\),/ \1/
tL
' file

Thank you sir Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output formatting problem

Hello; I have a simple loop filtering a log: for LU in $(< LU-list-Final) do OUT=$(grep -B1 $LU cibc-src-ip.cap |egrep 'IP 16|IP 19|IP 15' |awk -F">" '{print $1}') if ; then echo " LU $LU was accessed by ===============> $OUT " echo "" fi done The current output snippet looks like... (2 Replies)
Discussion started by: delphys
2 Replies

2. Shell Programming and Scripting

awk Script Output in Outlook Formatting Issue

When i execute the below shell script with 2 different Input files, for one of the data files (datafile1) my email message body in the outlook messes up and every thing comes up in one line. May i please know what i am doing wrong here or how to fix this? The only difference in data files is one is... (1 Reply)
Discussion started by: Ariean
1 Replies

3. Shell Programming and Scripting

Problem with output awk and sed

I have file, i am extracting email address from file. but problem is that output is very ugly. I am using this command REMOVED "CSS OFFENDING CODE"... While original filename have no such character. Please suggest. (20 Replies)
Discussion started by: learnbash
20 Replies

4. Shell Programming and Scripting

Formatting output using awk in ksh

:oi was trying to write a script to format output of a command in ksh which has output as below: so i used : to get which i require at all times. But problem occurs when status part changes. above output i get when status is SU (success).If the status is IN (inactive), output of... (1 Reply)
Discussion started by: pr5439
1 Replies

5. Shell Programming and Scripting

Formatting grep and awk output

Hi there. I have a very large file and I am trying to format it so that I can pull out certain pieces of data/info and report it in spreadsheet format/style. The file has ###### which will separate each line that will be listed in the spreadsheet. Whenever I find "No" at the end of a line I want... (7 Replies)
Discussion started by: kieranfoley
7 Replies

6. Shell Programming and Scripting

Formatting of output from awk

Hi; i m running an command like; getfacl $(find /home/fstl/testShell/ -type f -ls | awk '{print $11}') and i m getting outpiut for all files in "testShell" folder as; # file: home/fstl/testShell/.script1.sh.swp # owner: root # group: root user:fstl:rw- user:ajay:rw- group:root:r--... (1 Reply)
Discussion started by: ajaypadvi
1 Replies

7. Shell Programming and Scripting

output formatting with awk.

# echo $PATH /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin How would i write a script to display permission on each folders in $PATH variable below format. drwxr-xr-x 2 0 root 4096 Nov 24 14:51 /usr/kerberos/sbin drwxr-xr-x 2 0... (2 Replies)
Discussion started by: pinga123
2 Replies

8. Shell Programming and Scripting

output formatting problem

I would like to keep the complete lines in the output, but my script adds carriage returns for each space (e.g. keep BRITISH AIRWAYS on one line in the output): File1= BAW BRITISH AIRWAYS RYR RYAN AIR for i in $(cat File1) do echo $i done Output: BAW BRITISH AIRWAYS RYR... (4 Replies)
Discussion started by: barny
4 Replies

9. Shell Programming and Scripting

Question about formatting output using AWK

Hi everyone. I've got this fille... 0 5000001 5000002 5000003 5000006 5000007 5000219 11000156 11003130 11003132and this script... #!/bin/ksh FILE_ALERT_CONTACT_LIST=users.txt userID=`awk -F"=" '{printf $1 ", "}' $FILE_ALERT_CONTACT_LIST` (9 Replies)
Discussion started by: Fatbob
9 Replies

10. Linux

Output formatting problem..Plz hlp

Hi guys, It will be a great help if somebody can help me in following problem. I have tried hard but because of lack of UNIX/LINUX knowledge I am not able to do it. I have written a script that returns 3 things of all the employees in my organisation. i.e. Name, Login time & log out time of... (2 Replies)
Discussion started by: anushree.a
2 Replies
Login or Register to Ask a Question