help with Scripting - trying to search for string and extract next few characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with Scripting - trying to search for string and extract next few characters
# 1  
Old 01-13-2011
help with Scripting - trying to search for string and extract next few characters

Hi
I am new to world on unix scripting so any assistance would be gratefully appreciated,
I am trying to write a script which reads through a file, reads in line by line, searches for a pattern, copies string after it and then to do a search and replace elsehwere in the line,

so the input file would readsomething like
Code:
Value1:"alpha" Value2:"beta" Value3:"delta" Value4:"gamma" 
Value1:"bravo" Value2:"charlie" Value3:"echo" Value4:"sierra"

and i would want to extract the value3 from each line i.e "delta" and "echo" and replace Value2 with NewValue:extractedvalue Value2

so after runing the script the file shows
Code:
Value1:"alpha" NewValue:"delta" Value2:"beta" Value3:"delta" Value4:"gamma" 
Value1:"bravo" NewValue:"echo" Value2:"charlie" Value3:"echo" Value4:"sierra"


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you!

Last edited by Franklin52; 01-13-2011 at 05:57 AM..
# 2  
Old 01-13-2011
Code:
awk  '{o=$3;gsub(/.*:/,"",o); $2="Newvalue:"o" "$2}1' file


Last edited by ghostdog74; 01-13-2011 at 06:13 AM..
# 3  
Old 01-13-2011
Code:
awk '{split($3,a,":");$2="NewValue:" a[2] FS $2}1' file

# 4  
Old 01-13-2011
hi guys, thanks for your responses, but i should have mentioned the placement of where the values is in the string would vary, it would not necessarily be in the same position, so the only thing that is fixed is the search string i.e. Value3 and where to replace i.e. before Value2
and the below seems to be dependant on the position...
# 5  
Old 01-13-2011
Quote:
Originally Posted by LonJ_80
hi guys, thanks for your responses, but i should have mentioned the placement of where the values is in the string would vary, it would not necessarily be in the same position, so the only thing that is fixed is the search string i.e. Value3 and where to replace i.e. before Value2
and the below seems to be dependant on the position...
Code:
awk '{
  for(i=1;i<=NF;i++)
    if($i ~ "Value3:")
      split($i,a,":")
  $2="NewValue:" a[2] FS $2
}1' file

# 6  
Old 01-13-2011
Code:
gawk '{match($0,/Value3:(.[^ \t]*)/,a); $2="NewValue:"a[1] }1' file


Last edited by ghostdog74; 01-13-2011 at 09:28 AM..
# 7  
Old 01-13-2011
Quote:
Originally Posted by ghostdog74
Code:
awk '{match($0,/Value3:(.[^ \t]*)/,a); $2="NewValue:"a[1] }1' file

Use gawk for this solution, the third parameter of the match function is gawk specific.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract characters from a string name

Hi All, I am trying to extract only characters from a string value eg: abcdedg1234.cnf How can I extract only characters abcdedg and assign to a variable. Please help. Thanks (2 Replies)
Discussion started by: abhi_123
2 Replies

2. Shell Programming and Scripting

Need to extract characters between two search words in a script!!

Hi, I have a log file which is the output from a xml script : <?xml version="1.0" ?> <!DOCTYPE svc_result SYSTEM "MLP_SVC_RESULT_320.DTD"> <svc_result ver="3.2.0"> <slia ver="3.0.0"> <pos> <msid type="MSISDN" enc="ASC">8093078040</msid> <poserr> ... (4 Replies)
Discussion started by: arjunstarz
4 Replies

3. UNIX for Dummies Questions & Answers

Extract string between two special characters

Hi, I have a file that looks something like >1-18*anc... (12 Replies)
Discussion started by: jyu429
12 Replies

4. Shell Programming and Scripting

To Search for a string and to extract the string from the text

Hi Team I have an huge xml where i need to search for a ceratin numbers. For example 2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - <?xml version="1.0" encoding="UTF-8"... (5 Replies)
Discussion started by: Kannannair
5 Replies

5. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

6. Shell Programming and Scripting

Extract string between 2 special characters

Hi, I have a unix file with contents as below Line1: ABC MNN X$$QWERTY$$ JKL Line2: HELLO $$HOW$$ ARE $$YOU$$ DOING i want to extract the string between $$ and $$ ie i want the output as QWERTY HOW YOU i want those strings seperated by some character say | desired output is... (7 Replies)
Discussion started by: vinredmac
7 Replies

7. Shell Programming and Scripting

Search a String for characters - or +

How would I accomplish this if I want to test to see if it 1) starts with a dash so something like "-R" AND 2) starts with 1 character then either a "-" or "+" and then up to 3 characters such as "a+rx" (3 Replies)
Discussion started by: mkjp2011
3 Replies

8. Shell Programming and Scripting

Extract upper case characters from a string

Hi Friends, I have a script which returns me the following values line by line ora_smon_RCAT102 oracleKHP102B (DESC oraclePROOIDDB92 (DES oraclePMS (DESCRIP oracleBRIO (LOCAL=N oracleBRIO (LOCAL=N oraclePRDB92 (DES oracleBRIO (LOCAL=N oracleBRIO (LOCAL=N oraclePMS (DESCRIP... (4 Replies)
Discussion started by: orakhan
4 Replies

9. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

10. Shell Programming and Scripting

Extract string between two different characters

I want to extract a string from a line in a text file between two different characters: for example: :string" I want to extract string. It has to be done in a one-liner. (11 Replies)
Discussion started by: rein
11 Replies
Login or Register to Ask a Question