Script: splitting lines in multiple files and joining them
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
I thank Muha and /bin/bash for the replies. I must study what you have written. It will take another 48 hours or so. Because I am studying for a test in Russian. I have a test on Thursday. I am studying Russian nowadays.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
I have a file named ' lixt1.txt' on my system.
--------------------------------------------
#!/bin/bash
$ test='head -1 lixt1.txt'
$ echo $test
Mary Wednesday
$ echo ${test/ */}
Mary
$ echo ${test/Mary/Joe}
Joe Wednesday
$ echo ${test/W*/Joe}
Mary Joe
I created the above file and named it 'unwanted5'.
[nissanka@c83-250-104-186 ~]$ chmod 755 unwanted5
[nissanka@c83-250-104-186 ~]$ ./unwanted5
./unwanted5: line 4: $: command not found
./unwanted5: line 5: $: command not found
./unwanted5: line 6: Mary: command not found
./unwanted5: line 7: $: command not found
./unwanted5: line 8: Mary: command not found
./unwanted5: line 9: $: command not found
./unwanted5: line 10: Joe: command not found
./unwanted5: line 11: $: command not found
./unwanted5: line 12: Mary: command not found
[nissanka@c83-250-104-186 ~]$
What is the problem?
-----------------------------
[nissanka@c83-250-104-186 ~]$ cat lixt1.txt
Mary Wednesday
Joe Monday
Sally Saturday
You don't put a $ before the commands, e.g. if you want to use echo, the line is just something like
echo $test.
Also, if you're trying to put the output of a command into a variable (like it appears you want to do in the first line), then you can't just use those single quotes as that will put the string "head -1 lixt1.txt" into that variable (i.e. if you echo test, then the output will literally be "head -1 lixt1.txt"). I don't know how to do what it looks like you want to do, I don't really know any shell scripting stuff. Not sure what you're trying to do with the lines that say stuff like "Mary", or "Mary Wednesday" either.
Perhaps if you explain what your script is meant to do, then people can help.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
Thanks Nylex for taking time to reply me. I have changed the program. Please read the following:
#!/bin/bash
$ test='head -1 lixt1.txt'
echo $test
Mary Wednesday
echo ${test/ */}
Mary
echo ${test/Mary/Joe}
Joe Wednesday
echo ${test/W*/Joe}
Mary Joe
------------------------------------
The following is the output.
[nissanka@c83-250-104-186 ~]$ ./unwanted5
./unwanted5: line 4: $: command not found
./unwanted5: line 6: Mary: command not found
./unwanted5: line 8: Mary: command not found
./unwanted5: line 10: Joe: command not found
./unwanted5: line 12: Mary: command not found
[nissanka@c83-250-104-186 ~]$
Please explain what you are trying to do, as what you have written does not make sense as a program.
As Nylex said, do NOT precede cmds with unneeded chars eg '$'
If you want to save the results of the cmd
head -1 lixt1.txt
into a var eg test do it this way
test=`head -1 lixt1.txt`
NB those are backquotes, NOT single quotes ie (on my keybd) the key at the top left of the main keyblock, also has the '~' symbol on it.
Note also that 'test' is a bash keyword, so do not use it as a var.
Also show us what's in lixt1.txt.
You want to output the front of the file and place the result in a variable, evidently.
Your problem is that you are using single quotes around your head command, when you need to use backquotes (this character ` the unshifted tilde ~).
also, your variable test should not start with a $ when you are assigning it; you only prefix it with a $ when you use it. Also, no space between the $ and the variable name.
Further, your syntax defines Mary as a command on line 4. Unless you have a command named Mary on your system (I don't have on mine), your script will bomb at that point. Similarly for Joe.
@Gins: when i posted earlier, the commands i typed should be executed from the commandline and not from a script. When you see things like
Code:
$ test=`head -1 list1.txt`
The $ tells you that the command should be executed as a normal user, and from the commandline. In general: $ <command>
So when you want to run that command, just type on your commandline:
Code:
test=`head -1 list1.txt`
The other variant is # <command>
which tells you that the command should be executed as root user. Don't confuse $ <command> with calling a variable, which can be done like so: ${variable} or in our example ${test} or $test
Now what you wanted to do, is to use those commands in a script. When people post scripts they (hopefully) would include the first line #!/bin/bash, which makes it look something like: (this is the example i posted earlier as a script)
So when you see scripts posted like so, you can copy-paste them into a file and run it like you wanted to run ./unwanted5
Try copy-pasting this one and run it, does it work? Start learning more on this site: http://www.linuxcommand.org/ Good luck and post if you have problems
[nissanka@c83-250-104-105 ~]$ chmod 755 unwanted6
[nissanka@c83-250-104-105 ~]$ ./unwanted6
head -1 lixt1.txt
head
head -1 lixt1.txt
head -1 lixt1.txt
What is the problem now? The words Mary,Joe and Sally have gone to hell.
--------------------------------------------
The following is the 'lixt1.txt' file
[nissanka@c83-250-104-105 ~]$ cat lixt1.txt
Mary Wednesday
Joe Monday
Sally Saturday
You want to store the output of "head -1 list1.txt" in a variable called test, correct? You therefore need to use backticks, i.e. ` and not '. I'm sure this has been mentioned several times :/.
However, it didn't work the way it should work. Please read the following output.
[nissanka@c83-250-104-18 ~]$ vi unwanted6
[nissanka@c83-250-104-18 ~]$ chmod 755 unwanted6
[nissanka@c83-250-104-18 ~]$ ./unwanted6
head -1 lixt1.txt
head
head -1 lixt1.txt
head -1 lixt1.txt
[nissanka@c83-250-104-18 ~]$
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.