LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   add a word in end of lines & edit with +1 in every line (https://www.linuxquestions.org/questions/linux-newbie-8/add-a-word-in-end-of-lines-and-edit-with-1-in-every-line-912416/)

1Volt 11-08-2011 01:04 AM

add a word in end of lines & edit with +1 in every line
 
Welcome
--

let's say this is the text file
Code:

echo Play 1 > 1.txt
echo Play 1 > 2.txt
echo Play 1 > 3.txt

it's look like to add +1 and put .txt in every end of line

i want to do much like this
till 100.txt
how ?

and i have other text file
Code:

gedit 1.txt
gedit 2.txt
gedit 3.txt

same thing
to 100.txt

in other file i have something look like this
Code:

ABC
EFG
HIJ

i want to add in every end of line .jpg

just examples but i want to do different thing
please help ?
and srry for little speak

linuxwin2 11-08-2011 02:04 AM

You can write a script to do this.
Code:

#!/bin/bash
for ((i=1;i<=100;i++))
do
touch $i.txt
done


1Volt 11-08-2011 03:36 AM

thx for reply

but i mean in the text file
i don't wana create 1.txt 2.txt etc
i want to write 1.txt 2.txt in the text file
got it ?

lets say

i have text file ok
i want to make teams with a letter "A" then numbers then .US

A1.US
A2.US
A3.US
i want A -> till 100 .US
...
...
A100.US
i want just increase the number in every line

please answer the topic question 1.txt etc . and how to insert a word in end of lines
some of my friends says i should use sed but i dont know what the command to use to do that .

Please Help ><

grail 11-08-2011 04:26 AM

Same process just change touch for echo and redirect into your file.

David the H. 11-08-2011 10:55 AM

A shorter way to do it, using brace expansion

Code:

printf "%s" A{1..100}.US$'\n' > file.txt
$'\n' is a bash quoting pattern for inserting literal newlines into the string (see man bash -> QUOTING).

*Edit: changed it to use printf instead, as echo appears to also preserve the space inserted between each string by the brace expansion.


To add something to the end of every line in a file, try this:

Code:

sed -i 's/$/.jpg/' file.txt
-i tells it to edit the original file.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

1Volt 11-08-2011 04:36 PM

Thanks Guyz

but there 1 more thing i want to know how

the text file (1)
Code:

Thanks
Thanks
Thanks

i want to add in end of lines
number 1 increase by 1 in every line + .US

Code:

Thanks 1.US
Thanks 2.US
Thanks 3.US
Thanks 4.US

i tried to do that but fail

1Volt 11-08-2011 08:32 PM

Thanks anyway i did that by using Find and Replace :D
Thank u Very Much

linuxwin2 11-09-2011 02:45 AM

Code:


#!/bin/bash
nb=`wc -l file.txt|cut -d ' ' -f1`

for ((i=1;i<=$nb;i++))
do
      echo $(head -$i file.txt|tail -1) $i.US >>tmp.txt
done


David the H. 11-09-2011 08:54 AM

sed doesn't have incrementing ability, but you can do it with awk.

Code:

awk 'BEGIN{ i=1 } { print $0 " " i ".US" ; i++ }' file.txt
BEGIN{ i=1 } presets the variable i to a starting value. This can be left out if you want the count to start at 0.

print $0 prints the entire input line, which is then followed by a space, the current value of i, and the rest of the desired string. (Note that in the print command literal strings must be quoted, and any unprotected spaces between the values are ignored.)

Finally, i++ increments a variable by one for each line processed. (Use i+=2, for example, to step by other increments)

However, awk doesn't have inline editing ability, so you have to send the output to a separate file.

And here's another variation for creating a file with the desired sequence (10 lines, in this case).
Code:

awk 'BEGIN{ for ( i=1; i<=10; i++ ){ print "Thanks " i ".US" } }' > file.txt
awk is the most powerful of the text processing tools generally used in scripting. It is, in fact, a full scripting language of its own.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

I highly recommend learning at least the basics of sed, and awk, as well as regular expressions, which are used by them and many other applications. They are the backbone tools of text processing.

colucix 11-09-2011 08:59 AM

Another one:
Code:

awk '$0 = sprintf("%s %d.US",$0,++c)' file

1Volt 11-11-2011 09:42 AM

Thx guyz

David the H. 11-11-2011 01:21 PM

Nifty version colucix. It just goes to show that there are many ways to accomplish the same thing.

I was trying to keep it to a basic level for my example. We don't want to get in too deep with a new user. That kind of thing comes with experience.

colucix 11-11-2011 03:03 PM

Quote:

Originally Posted by David the H. (Post 4521735)
I was trying to keep it to a basic level for my example. We don't want to get in too deep with a new user. That kind of thing comes with experience.

I agree. On the other hand I hope to arouse the curiosity and bring the learners to read the documentation (or ask for clarification) and do their research trying to figure out why such a bare and clean command actually works! :jawa:


All times are GMT -5. The time now is 04:48 AM.