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
# 1  
Old 04-07-2017
Wrench Problem in formatting output in sed / awk

I have a file like this :
Code:
!    1 ! 542255 !    50,140.00  !
!    2 ! 551717 !     5,805.00  !
!    3 ! 551763 !     8,130.00  !
!    4 ! 551779 !       750.00  !
!    5 ! 551810 !    56,580.00  !
!    6 ! 551816 !     1,350.00  !
!    7 ! 551876 !       360.00  !
!    8 ! 551898 !     6,580.00  !
!    9 ! 557285 !    69,295.00  !
!   10 ! 557508 !     6,685.00  !

I have used sed to remove the thousands separator
Code:
sed 's/,//g' file

The column of amount now gets mis-aligned, like this :
Code:
!    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  !

The numbers with thousands separator gets shifted one space left. And if the numbers are in lakhs, the more it shifts to the left. I need the output like this :
Code:
!    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  !

Please help me to do it either using sed or awk. I prefer awk

Last edited by adam1969in; 04-07-2017 at 11:40 PM.. Reason: spelling mistake
# 2  
Old 04-08-2017
Code:
awk -F! '{if(sub(",", "", $4)){$4=" "$4}}1' OFS=! adam1969in.file

Code:
 sed 's/\([0-9]*\),/ \1/'  adam1969in.file

Code:
perl -pe 's/(\d+),/ $1/' adam1969in.file

This User Gave Thanks to Aia For This Post:
# 3  
Old 04-08-2017
A different approach that should also work if there is more than one thousands separator:
Code:
awk '{$4=sprintf("%*s",gsub(/,/,x,$4),x)$4}1' FS=! OFS=! file

--
x represents an empty string here. The number of comma replacements (gsub) determine the amount of space padding to the left.


--
As an alternative, if the column width is known beforehand, then one could also use:
Code:
awk '{gsub(/,/,x,$4); $4=sprintf("%15s",$4)}1' FS=! OFS=! file

If it is not, it can be determined like this:
Code:
awk '{l=length($4); gsub(/,/,x,$4); $4=sprintf("%*s",l,$4)}1' FS=! OFS=! file


--
Note:
The locale's thousands separator number parsing is broken in many applications, so the value needs to be treated as a string instead of a number. For example bash and awk fail to interpret the number in column 4 correctly..


ksh93 for one does it perfectly however (with the right locale):
Code:
LC_NUMERIC="en_US.UTF-8"
while IFS=! read -A f
do
  printf "%s!%s!%s!%13.2f  !\n" "${f[@]}"
done < file


Last edited by Scrutinizer; 04-08-2017 at 09:34 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-08-2017
Code:
 sed 's/\([0-9]*\),/ \1/'  adam1969in.file

This one is short and sweet. It does what I need. What if I have more than one columns with thousands separator.

Thank you very much sir.

Last edited by RudiC; 04-08-2017 at 06:16 AM.. Reason: Changed QUOTE to CODE tags.
# 5  
Old 04-08-2017
sed might not be the tool of choice, then. Use either of the awk proposals and loop across the columns in question.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 04-08-2017
Quote:
Originally Posted by RudiC
sed might not be the tool of choice, then. Use either of the awk proposals and loop across the columns in question.
Yes, you are right sir. Using the formula given by @Scrutinizer
Quote:
Code:
awk '{l=length($4); gsub(/,/,x,$4); $4=sprintf("%*s",l,$4)}1' FS=! OFS=! file

I have used this for loop. It is working perfectly. However, please check if I have used it correctly.
Code:
awk 'BEGIN{
FS="!"; OFS="!"
}
{
for (i=1;i<=NF;i++){
l=length($i); gsub(/,/,x,$i); $i=sprintf("%*s",l,$i)}1
print}' file


Last edited by Scrutinizer; 04-08-2017 at 09:22 AM.. Reason: Extra code tags
# 7  
Old 04-08-2017
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)

Last edited by Scrutinizer; 04-08-2017 at 09:23 AM..
This User Gave Thanks to Scrutinizer For This Post:
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