LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2011, 02:04 AM   #1
1Volt
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Rep: Reputation: Disabled
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
 
Old 11-08-2011, 03:04 AM   #2
linuxwin2
Member
 
Registered: Oct 2011
Posts: 44

Rep: Reputation: Disabled
You can write a script to do this.
Code:
#!/bin/bash
for ((i=1;i<=100;i++))
do
touch $i.txt
done
 
1 members found this post helpful.
Old 11-08-2011, 04:36 AM   #3
1Volt
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: Disabled
Unhappy

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 ><
 
Old 11-08-2011, 05:26 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031

Rep: Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202
Same process just change touch for echo and redirect into your file.
 
Old 11-08-2011, 11:55 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

Last edited by David the H.; 11-08-2011 at 12:02 PM.
 
1 members found this post helpful.
Old 11-08-2011, 05:36 PM   #6
1Volt
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-08-2011, 09:32 PM   #7
1Volt
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks anyway i did that by using Find and Replace
Thank u Very Much
 
Old 11-09-2011, 03:45 AM   #8
linuxwin2
Member
 
Registered: Oct 2011
Posts: 44

Rep: Reputation: Disabled
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
 
Old 11-09-2011, 09:54 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 11-09-2011, 09:59 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Another one:
Code:
awk '$0 = sprintf("%s %d.US",$0,++c)' file
 
1 members found this post helpful.
Old 11-11-2011, 10:42 AM   #11
1Volt
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thx guyz
 
Old 11-11-2011, 02:21 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 11-11-2011, 04:03 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Quote:
Originally Posted by David the H. View Post
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed append word at end of line if word is missing franjo124 Linux - Newbie 3 03-08-2012 09:41 PM
[SOLVED] Using Vim to add something to the end of a series of a lines Vim-Newbie Linux - Newbie 3 05-23-2011 03:47 PM
Add comma to end of lines in text file Johng Programming 9 08-21-2010 05:15 AM
[SOLVED] Need to edit last word on each line in file emmalg Linux - General 10 06-01-2010 11:36 AM
print second word in 1st line along with 5th word in all the lines after the first bangaram Programming 5 08-31-2009 04:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration