keep only a string from line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting keep only a string from line
# 1  
Old 07-07-2012
keep only a string from line

hello everyone

I'm writing a bash script and I have to parse a line that looks as follows:

Code:
a/ b:4 word=egg things.?/things2//

no matter what other words or characters exist in the line,I only want to keep the word(or number) that comes after the =
in this case the word is "egg".
Is there a way doing this by using sed?
# 2  
Old 07-07-2012
Does it have to be sed?
Code:
echo 'a/ b:4 word=egg things.?/things2//' | perl -nle '/=(\w+)/&&print $1'

# 3  
Old 07-07-2012
Quote:
Originally Posted by bartus11
Does it have to be sed?
Code:
echo 'a/ b:4 word=egg things.?/things2//' | perl -nle '/=(\w+)/&&print $1'

no it doesn't have to be sed.The only thing is that is a bash script.
Is this a problem for using perl?
This User Gave Thanks to vlm For This Post:
# 4  
Old 07-07-2012
Perl will work just fine in a bash script.
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-07-2012
You can also use the following :

Code:
echo "a/ b:4 word=egg things.?/things2//" | awk -F= '{print $2}' | cut -d' ' -f1

# 6  
Old 07-07-2012
If you already have that line in a shell variable, you do not need to resort to sed, awk, or perl. Just use the shell's parameter expansion operators:
Code:
x='a/ b:4 word=egg things.?/things2//'
y=${x#*=}
y=${y%% *}

x contains the original line. y contains only the word/number sought.

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

2. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

Grep a string and write a value to next line of found string

Hi, I have two variables x and y. i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name. basically it is like as below wf_xxxxxx $$a= $$b= $$c= figo $$d=bentley i need to grep the 'wf_xxxx' and then... (6 Replies)
Discussion started by: angel12345
6 Replies

8. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

9. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies
Login or Register to Ask a Question