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 10-20-2009, 01:54 AM   #1
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Rep: Reputation: 18
shell scripting on linux


hi,

I have just started my first experience with shell scripting, i want to write a script to open a text file and in that file copy some lines or repeat some lines in the same file. how do i do that, i mean what is the command to open a text file and edit it ?

what is the command for copying lines i tried 'co' command but its not working, which i had seen in one of the tutorials on Google.

please help me
 
Old 10-20-2009, 02:35 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
there are many many many commands to do all sorts of things, sed, awk, vi, cat, grep, echo... use whichever you prefer. check out the bash scripting howto as a good starters guide in scripting in general
 
Old 10-20-2009, 03:15 AM   #3
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
Thanks acid_kewpie . But i want to know if there is any option for repeating a particular line, along with all the other file contents...
please help me
 
Old 10-20-2009, 03:17 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
It depends which tool you use; have a read of these:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
 
Old 10-20-2009, 04:54 AM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by ibabhelix View Post
Thanks acid_kewpie . But i want to know if there is any option for repeating a particular line, along with all the other file contents...
please help me
Here is a script that reads lines from a file, then reprints those lines as many times as the "repeats" variable requires:

Code:
repeats=5

cat temp.txt | while read line
do
   for ((i = 1; i <= repeats;i++))
   do
      echo "$i: $line"
   done
done
Now I ask you -- what is the most important thing about this script? It's not that it works (that's easy), it is that it can be understood, and five years from now, you will still be able to read it and understand it.

Rule one in programming: pay attention to the comprehensibility factor -- will you be able to read, understand, and reuse your programs after some time has passed?
 
Old 10-20-2009, 05:04 AM   #6
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
please tell me how to use for loop with sed command is it like this ?
Code:
for((i=1;i<=10;i++))
do
for((j=2;j<=10;j++))
do
sed -n '$i,$j p' <2K6.rtf > z
done
done
 
Old 10-20-2009, 06:34 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
You seem to be asking random questions and it's not clear how they are all related.

Above, you have a nested loop, which looks fine. What happens when you run it?
The only thing that I notice is that you need to use double quotes in the sed statement so that the 2 variables will be expanded:
Code:
sed -n "$i,$j p" <2K6.rtf > z
You nested loop **looks like** it will try to print the following line ranges:
1-2
1-3
1-4
.....etc.
2-2
2-3
2-4
.....etc.
3-2
3-3
3-4
...etc.
4-2
4-3
4-4
...etc.

I hope you can see that some of these do not make sense.

All this aside, please post some kind of description of the problem that ties all this together, and then show us the complete code so we can see the whole picture.
 
Old 10-20-2009, 06:37 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
PS: If you have not read the BASH Beginners Guide (link posted by @chrism01), please do so before you try to go too much further.
 
Old 10-20-2009, 06:48 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ibabhelix View Post
hi,
i want to write a script to open a text file and in that file copy some lines or repeat some lines in the same file. how do i do that, i mean what is the command to open a text file and edit it ?
normal way to open file in bash (note:no need cat)
Code:
while read -r line
do
  echo "do something with $line"
done < "file"
also, depending on what other programming languages you know..there is definitely a way to edit files. The problem is whether you want to read the docs or not. ....
 
Old 10-20-2009, 06:48 AM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
pixellany, context is for wimps, ok?
 
Old 10-20-2009, 06:51 AM   #11
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
see ok let me put my doubt in this way i have two files like this
Code:
ATOM      3  C4'   G A   1      
ATOM     33  P     G A   2       
ATOM     38  C4'   G A   2     
ATOM     67  P     A A   3      
ATOM     72  C4'   A A   3      
ATOM    100  P     G A   4       
ATOM    105  C4'   G A   4      
ATOM    134  P     U A   5        
ATOM    139  C4'   U A   5
Code:
ATOM     33  P     G A   2          
ATOM     67  P     A A   3      
ATOM    100  P     G A   4           
ATOM    134  P     U A   5        
ATOM    164  P     A A   6
and want to write a script such that i can get 2lines of my first file then one line of second line like this:
Code:
ATOM      3  C4'   G A   1 
ATOM     33  P     G A   2   
ATOM     33  P     G A   2         
ATOM     38  C4'   G A   2     
ATOM     67  P     A A   3  
ATOM     67  P     A A   3  
ATOM     72  C4'   A A   3      
ATOM    100  P     G A   4
I hope my question is clear to u now sir, please help me.
how do i do this ?
 
Old 10-20-2009, 07:25 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I hope you can see the problem caused by not giving us the complete problem up front......

You need two nested loops which read from the files, and also keep track of what line numbers are to be read.

<<Edit: I found a problem in this---see my post below>>

Finally, please confirm that you have read the BASH Guide for Beginners as we suggested.

Last edited by pixellany; 10-20-2009 at 02:26 PM.
 
Old 10-20-2009, 07:40 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/bin/bash
# done with Bash 4.0
exec 6<"file1" # assign file descriptor 6 to file1
exec 7<"file2" # assign file descriptor 7 to file2
while read  a <&6 # read a line from file1
do
    read  b <&6 # read the 2nd line from file1
    echo "$a"  # print both lines from file1
    echo "$b"
    read c <&7  # read one line from file2. Note &7 doesn't have its own while loop, 
                # therefore only 1 line is read inside &6's while loop.
    echo "$c"  # print out the line
done
exec >&6- 
exec >&7- #close file descriptor
output
Code:
# ./shell.sh
ATOM      3  C4'   G A   1
ATOM     33  P     G A   2
ATOM     33  P     G A   2
ATOM     38  C4'   G A   2
ATOM     67  P     A A   3
ATOM     67  P     A A   3
ATOM     72  C4'   A A   3
ATOM    100  P     G A   4
ATOM    100  P     G A   4
ATOM    105  C4'   G A   4
ATOM    134  P     U A   5
ATOM    134  P     U A   5

Last edited by ghostdog74; 10-20-2009 at 08:32 AM.
 
Old 10-20-2009, 07:56 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I found a problem in what I posted. Ghostdog's solution may be more elegant, but I'm having a hard time following it....for example, where is the merged data being written to?

"while read" creates a loop that reads from standard input until it reaches end of file. But does it also terminate the loop when the second read statement reaches end of file? A simple test will verify this.

Also, I don't think you need the file desciptors---ie you can just use the redirects with the actual file names?
 
Old 10-20-2009, 08:37 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
I found a problem in what I posted. Ghostdog's solution may be more elegant, but I'm having a hard time following it....for example, where is the merged data being written to?
since there are only echo's in my script, it naturally goes to standard output.

Quote:
"while read" creates a loop that reads from standard input until it reaches end of file. But does it also terminate the loop when the second read statement reaches end of file? A simple test will verify this.
my solution assumes file1 has more data than file2 as per OP's sample inputs, if not, the while loop for the first file will terminate. If that doesn't fit OP's requirement, then he should not use the script.

Quote:
Also, I don't think you need the file desciptors---ie you can just use the redirects with the actual file names?
in this case, yes i need as i am not iterating file2 inside while loop of file1. i am using file1's loop instead. If an extra while loop is introduced for file2, the logic will be different.

I was feeling nostalgic about bash so i did it for "fun"... awk is still what i would use...

Last edited by ghostdog74; 10-20-2009 at 08:40 AM.
 
  


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
Linux Shell Scripting latinmusic74 Linux - Newbie 13 09-27-2008 03:27 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
Linux Shell Scripting yaadhav Linux - Newbie 4 08-20-2005 04:05 PM
Linux Shell Scripting Help RowanB Programming 13 11-12-2004 05:07 PM


All times are GMT -5. The time now is 12:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration