0

So what I'm trying do do is make a script to read my input file and do stuff accordingly.

My input file gets sent to me in this format:

ID     QTY
xxxxxxxxx,xxx
xxxxxxxxx,xx
xxxxxxxxx,xxx
xxxxxxxxx,xx
xxxxxxxxx,xx

Sometimes the ID is only 8 digits because of the number being smaller. I need it to be formatted with leading zeros if that occurs. Also, my input file has thousands of lines.

I have this so far

echo "${processNew}"

## Read the file line-by-line and output the id.
IFS=','
while read line
do 
    echo "%09d\n" $line

done < ${processNew}
2
  • you want to format xxxxxxxxx,xxx or xxxxxxxxx , the first part only? Commented Jun 9, 2016 at 17:10
  • Format a line to be just like this xxxxxxxxx,xxx - where the last 3 x's is the quantity and can freely be more or less. Commented Jun 9, 2016 at 19:54

1 Answer 1

1

EDIT: You are almost there, just need a little tweak in your code, but without loop :)

write like this if wanna print both the columns

awk -F, '{printf "%09d,%d\n" ,$1,$2}' "${processNew}"

write like this if wanna print Only ID column

awk -F, '{printf "%09d\n" ,$1}' "${processNew}"
Sign up to request clarification or add additional context in comments.

6 Comments

Using awk is correct, but running awk once per line of input is a nonsense. You should lose the shell loop, and simply provide the input file as a double-quoted argument to awk — leading to: awk -F, '{printf("%.9d,%d\n", $1, $2)}' "${processNew}" where the parentheses are optional and the use of %.9d is equivalent to %09d (though there are inscrutable arguments for preferring %.9d, they're subtle and not critical). The unindented presentation of your code is less than desirable too.
@JonathanLeffler - you are right, Thanks for letting me know. Edited the answer
@sayadav - where would I put that code in mine? You mentioned losing the loop?
@JonathanLeffler - could you help me out implementing your code to mine?
@Adam Horn - you should consider using above code only. Leave the shell you are using . Just assign your file name to variable processNew. It will work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.