简体   繁体   中英

Assigning a column with 100 char spaces while printing it in output text file using Shell Scripting

i just want to fetch lines from a csv file and print it line by line. the thing is the below code fetches the 4th column and prints it. What i want to do is, i just want to give 100 char spaces for that column printing. if that column has 10 chars i just wanna print that and rest 90 chars spaces should be empty in that line.

cat filename | awk -F"," '{print $4}'

AWK has printf() function that support format. With format "%100s" and "%-100s" you can pad printed string with spaces from left or right:

 $ echo "a\nb\nc\nd" | awk '{ printf("|%10s|\n", $1);}'                                  
 |         a|
 |         b|
 |         c|
 |         d|
 $ echo "a\nb\nc\nd" | awk '{ printf("|%-10s|\n", $1);}'                                    
 |a         |
 |b         |
 |c         |
 |d         |
 $

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM