Extract text from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract text from string
# 8  
Old 09-05-2012
You have missed $ in the code...

Quote:
Originally Posted by Lord Spectre
Well, this will drive me crazy:

Code:
INFO=$(awk '/INFO/{sub(/^ */,"");print;}' /info.tmp)
echo $INFO
echo "$FIELD1 ; $FIELD2 $INFO ; $FIELD4"

# 9  
Old 09-05-2012
Quote:
Originally Posted by zaxxon
Code:
# INFO="$(sed '/INFO/ {s/^ *//}' info.tmp)"
# echo "FIELD1; FIELD2; $INFO; FIELD4"
FIELD1; FIELD2; INFO : ABCDEFG; FIELD4

No way, this will remove the leading space for INFO row, but it will report all the rest from info.tmp file... :-(

@pamu, this is just a test and I report only a sinple text! Smilie
# 10  
Old 09-05-2012
Ok, then try this:
Code:
# INFO="$(sed -n '/INFO/ {s/^ *//p}' info.tmp)"
# echo "FIELD1; FIELD2; $INFO; FIELD4"
FIELD1; FIELD2; INFO : ABCDEFG; FIELD4

This User Gave Thanks to zaxxon For This Post:
# 11  
Old 09-05-2012
Quote:
Originally Posted by zaxxon
Ok, then try this:
Code:
# INFO="$(sed -n '/INFO/ {s/^ *//p}' info.tmp)"
# echo "FIELD1; FIELD2; $INFO; FIELD4"
FIELD1; FIELD2; INFO : ABCDEFG; FIELD4

Ok this extract only the INFO field, but why I have this output?!?!?!?! Smilie

Code:
INFO="$(sed -n '/INFO/ {s/^ *//p}' info.tmp)"
echo $INFO
echo "FIELD1 ; FIELD2 ; $INFO ; FIELD4"

Result:

Code:
# ./test.sh 
INFO : ABCDEFG
  ; FIELD4FIELD2 ; LA_INFO : ABCDRFG

# 12  
Old 09-05-2012
Try with this...

Code:
INFO=$(grep -i INFO /info.tmp | sed 's/^ *//')

This User Gave Thanks to pamu For This Post:
# 13  
Old 09-05-2012
OK, THAT'S CRAZY!!!

This works, but I believe work also the other solution:
Code:
INFO=$(grep -i INFO /info.tmp | sed 's/^ *//')
echo "AAA ; BBB ; CCC ; $INFO"

Output:
Code:
# ./test.sh 
AAA ; BBB ; CCC ; INFO : ABCDEFG

This doesn't work, and I don't understand why:
Code:
INFO=$(grep -i INFO /info.tmp | sed 's/^ *//')
echo "AAA ; BBB ; CCC ; $INFO DDD"

Output:
Code:
 DDD; BBB ; CCC ; INFO : ABCDEFG

You see? I only add a simple text after $INFO variable!!!!
# 14  
Old 09-05-2012
Quote:
Originally Posted by Lord Spectre
INFO=$(grep -i INFO /info.tmp | sed 's/^ *//')
echo "AAA ; BBB ; CCC ; $INFO DDD"[/CODE]
Output:
Code:
 DDD; BBB ; CCC ; INFO : ABCDEFG

You see? I only add a simple text after $INFO variable!!!!
I am not getting what's problem with your bash..Smilie

here i am getting perfect...

Code:
$ cat file3.sh
INFO="INFO : ABCDEFG"
echo "AAA ; BBB ; CCC ; $INFO DDD"

$ ./file3.sh
AAA ; BBB ; CCC ; INFO : ABCDEFG DDD

and in your previous post also... If you are getting $INFO right it should work for all the scenarios..Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract all text between the same matching string from a given column

Hello All, I have an input sample data like below (In actual I have many columns and few million rows). Column1,Column2 4,2 1,5 Hello,4 1,4 Hello,2 3,5 Hello,8 4,5 Need the output (using awk and/or sed preferably) like below. Here I need all the lines between 2 matching... (1 Reply)
Discussion started by: ks_reddy
1 Replies

2. 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

3. 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

4. Shell Programming and Scripting

How to extract text from STRING to end of line?

Hi I have a very large data file with several hundred columns and millions of lines. The important data is in the last set of columns with variable numbers of tab delimited fields in front of it on each line. Im currently trying sed to get the data out - I want anything beetween :RES and... (4 Replies)
Discussion started by: Manchesterpaul
4 Replies

5. Shell Programming and Scripting

Extract a string between 2 ref string from a file

Hi, May i ask if someone share some command for extracting a string between 2 ref string in a txt file My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per... (4 Replies)
Discussion started by: jao_madn
4 Replies

6. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

how to extract a paticular string from the text file with awk.

hello forum members I have txt file which consists the following information. Server: abababa.xyz.ap.mxmx.com Address: 111.143.211.202 Name: rmxd.ipc.ap.mxmx.com Address: 144.111.99.9 from the abovefile i have to extract only string "rmxd.ipc.ap.mxmx.com" through awk command.... (1 Reply)
Discussion started by: rajkumar_g
1 Replies

8. 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

9. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

10. Shell Programming and Scripting

How to extract text from string using regular expressions

Hi, I'm trying to use sed to extract some text and assign it to a variable. Can anyone provide me with some help? it would be much appreciated! I"m looking to extract for example: filename=/output/R34/2005_13_R34_C1042S_T83_CRFTXT_20081015.txt I'm trying to extract the 1042... (9 Replies)
Discussion started by: jtung
9 Replies
Login or Register to Ask a Question