Copying a string from a file using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying a string from a file using shell script
# 1  
Old 04-17-2013
Linux Copying a string from a file using shell script

Hello everyone

I am completely new to shell scripting in linux. I wan to write a script to search for a certain string from a .txt file and copy the string which apears just after tat searched string.

Eg: in a file- try.txt , we have a line saying: "roses are red, so what do i do"

I want ot search for the string "roses" and then copy the next string i.e. "are" and assign it to a variable.

Could anyone help me with this.
# 2  
Old 04-17-2013
Like this since you are in Linux:

Code:
$ cat file
roses are red, so what do i do
nothing, just get the next string

Code:
$ var=$(grep -oP '(?<=roses )\S+' file)
$ echo $var
are

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 04-17-2013
Hey!!!

Welcome to the Unix and Linux Forum!!!!

Try someting like this,

First of all, you haven't mentioned, What shell you are using, Here you go with bash.

Code:
var=$(sed 's/.*roses \([^ ]*\).*/\1/g' try.txt)
echo $var

Please use code tag for code samples and data samples in future.

Thanks,

Cheers,
Ranga Smilie
This User Gave Thanks to rangarasan For This Post:
# 4  
Old 04-17-2013
Code:
$ cat temp.sh
var=`awk '{ for (i=1; i<NF; i++) { if ($i ~ /roses/) { i++; print $i; exit }}}' try.txt`
echo $var

Code:
$ ./temp.sh
are

# 5  
Old 04-17-2013
This work perfectly for me. Thanks a lot. :)

Quote:
Originally Posted by guruprasadpr
Like this since you are in Linux:

Code:
$ cat file
roses are red, so what do i do
nothing, just get the next string

Code:
$ var=$(grep -oP '(?<=roses )\S+' file)
$ echo $var
are

Guru.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Search a string in a file in shell script?

I have a text file which is generated when the batch job is run. This batch may take few mins to run. When completed, the last line of the text file would be process completed. I need a shell script which will wait for this file till the process completed is printed in it, once found, it would move... (2 Replies)
Discussion started by: Lalat
2 Replies

2. Shell Programming and Scripting

Shell script to search all files for every string in another file

Hello All I have a pattern.txt file in source directory ((/project/source/) in linux server and data looks like: 123abc17 234cdf19 235ifg20 I have multiple log files in log directory (/project/log/) in linux server and data for one log file looks like: <?xml version="1.0" processid... (11 Replies)
Discussion started by: pred55
11 Replies

3. Shell Programming and Scripting

UNIX shell script to search a string in a file

Hi folks, I am new for shell script, I hope somebody to help me to write shell script My requirement is below steps 1. I have apache access.log i.e located in /var/log/httpd/ Ex. 127.0.0.1 - - "GET... (14 Replies)
Discussion started by: Chenchireddy
14 Replies

4. Shell Programming and Scripting

Shell Script for copying text file to Excel Sheet

Hi, I want to write a program to copy a log file to Excel sheet. Excel sheet has four columns MethodName , Code , Description, Details and Time. I want to pick these info from text file and put it in excel sheet. here is how the text file looks - 04.17.2014 08:06:12,697... (1 Reply)
Discussion started by: hershey
1 Replies

5. Shell Programming and Scripting

Help with copying files using shell script

I want to write a shell script to copy a list of files from one directory to another. And while copying it should change the first character of the filename to uppercase and others to lowercase.Below is what i have tried so far. for file in "$@" do if then ufile=`echo $file | sed... (5 Replies)
Discussion started by: vishal.desai
5 Replies

6. Shell Programming and Scripting

Shell script for copying files from 1 server to other

Hi, I just need a shell script that copies a list of files from a directory in a remote server to my current directory at local server the remote server may contain the following list: /root/pradeep/myfiles/default /root/pradeep/myfiles/dir1 /root/pradeep/myfiles/dir2 ...... (1 Reply)
Discussion started by: paddu
1 Replies

7. Shell Programming and Scripting

Shell Script to extract string from file

Hello, I have a file whose contents look as follows <ID="24" name="Kobe Bryant" Position="Shooting Guard"> <name="Artest" Position="Forward" ID="37"> I need to get the output which contains everything in the quotes for Position (Shooting Guard and Forward). I tried this but it doesn't work... (3 Replies)
Discussion started by: grajp002
3 Replies

8. Shell Programming and Scripting

Shell script not unzipping and copying correctly

Hi, I am trying to unzip a file( $pfile, it contains a couple of files and 4 folders with subfolders and files) and have its contents go into a directory instead of into a folder in that directory (ZZZZ), I have the following script: #Unzip the build unzip -o "$HOME/ZZZZ/$pfile" -d... (2 Replies)
Discussion started by: SlumberMachine
2 Replies

9. Shell Programming and Scripting

Parse a string in XML file using shell script

Hi! I'm just new here and don't know much about shell scripting. I just want to ask for help in creating a shell script that will parse a string or value of the status in the xml file. Please sample xml file below. Can you please help me create a simple script to get the value of status? Also it... (46 Replies)
Discussion started by: ayhanne
46 Replies

10. Shell Programming and Scripting

copying files and Renaming them + shell script

Hi, I have a problem. I have some text files in a folder. The names can be like: emp.txt emp1.txt emp3.txt 32emp4.txt What i need is i have to copy all the files which have "emp" string in their filename to a different folder and those file names... (7 Replies)
Discussion started by: pathanjalireddy
7 Replies
Login or Register to Ask a Question