LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripting on linux (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-on-linux-763116/)

ibabhelix 10-20-2009 12:54 AM

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

acid_kewpie 10-20-2009 01:35 AM

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

ibabhelix 10-20-2009 02:15 AM

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

chrism01 10-20-2009 02:17 AM

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

lutusp 10-20-2009 03:54 AM

Quote:

Originally Posted by ibabhelix (Post 3725737)
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?

ibabhelix 10-20-2009 04:04 AM

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


pixellany 10-20-2009 05:34 AM

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.

pixellany 10-20-2009 05:37 AM

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.

ghostdog74 10-20-2009 05:48 AM

Quote:

Originally Posted by ibabhelix (Post 3725665)
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. ....

acid_kewpie 10-20-2009 05:48 AM

pixellany, context is for wimps, ok?

ibabhelix 10-20-2009 05:51 AM

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 ?

pixellany 10-20-2009 06:25 AM

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.

ghostdog74 10-20-2009 06:40 AM

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


pixellany 10-20-2009 06:56 AM

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?

ghostdog74 10-20-2009 07:37 AM

Quote:

Originally Posted by pixellany (Post 3725931)
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...;)


All times are GMT -5. The time now is 03:40 AM.