LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Append numbers to a word list? cat... (https://www.linuxquestions.org/questions/programming-9/append-numbers-to-a-word-list-cat-853265/)

NothingToLose 12-30-2010 09:18 AM

Append numbers to a word list? cat...
 
If I do:
for i in $(cat names); do seq -f "$i%03.0f" 0 999; done > output5.txt
I got ( tail snipped):
997munt
998munt
999munt
If I do:
for i in $(cat names); do seq -f "%03.0f$i" 0 999; done > output4.txt
I got:
997Zygmunt
998Zygmunt
999Zygmunt
How I can get numbers from 000 to 999 appended like:
Zygmunt997
Zygmunt998
Zygmunt999

catkin 12-30-2010 09:27 AM

Where did the "Zyg"s come from in the second example?

NothingToLose 12-30-2010 10:28 AM

Correct word on the list is with "Zyg" Zygmunt etc... I wonder more where the Zyg disapperead & why the number did not come behind?

NothingToLose 12-30-2010 10:41 AM

Or is it the list which makes problems?

A'Leshia
Aarika
Aaron
Abas
Adrian Belew
Adriana
Adrianna
Zvi
Zvonko
Zygmunt

ntubski 12-30-2010 10:46 AM

Works for me:

Code:

$ cat names
A'Leshia
Aarika
Aaron
Abas
Adrian Belew
Adriana
Adrianna
Zvi
Zvonko
Zygmunt
$ for i in $(cat names); do seq -f "%03.0f$i" 0 999; done | tail -3
997Zygmunt
998Zygmunt
999Zygmunt
$ for i in $(cat names); do seq -f "$i%03.0f" 0 999; done | tail -3
Zygmunt997
Zygmunt998
Zygmunt999


NothingToLose 12-30-2010 11:14 AM

There has to be something strange on my Shell? Ubuntu Lucid...

Kenhelm 12-30-2010 11:25 AM

The names file could contain carriage return characters if it was made in Windows.
\n is the end of line code for Linux
\r\n is the end of line code for Windows
\n represents a newline character
\r represents a carriage return character
Code:

# $'\r' gives a carriage return character in bash
i=Zygmunt$'\r'  # append a carriage return character

echo "${i}999"
999munt

# Using bash parameter expansion to remove the carriage return
echo "${i%$'\r'}999"
Zygmunt999


NothingToLose 12-30-2010 01:06 PM

Thanks! It was the carriage return. I did: tr -d '\r' < names.txt > newNames.txt

grail 12-30-2010 08:36 PM

Please mark as SOLVED once you have a solution :)


All times are GMT -5. The time now is 01:12 AM.