Extract text from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract text from string
# 1  
Old 09-05-2012
Extract text from string

Dear community, I know, this is very stupid question, but I'm scratching my head to find a solution.

I have a variable like this:
Code:
var="                       INFO : ABCDEFG"

Now I need to remove the leading spaces and output the result like:

Code:
echo "FIELD1 ; FIELD2 ; $RESULT ; FIELD4"

$RESULT should be = "INFO : ABCDEFG" Could someone help me?

Last edited by zaxxon; 09-05-2012 at 08:46 AM.. Reason: adding more code tags
# 2  
Old 09-05-2012
Basically one of many ways to remove the leading blanks is:
Code:
echo "$var"| sed 's/^ *//'
INFO : ABCDEFG

Though how is this related to the line with echoing the fields? I added some more code tags since you did not use them for the result, but maybe you explain this a tad more in detail.
# 3  
Old 09-05-2012
In ksh:
Code:
RESULT="                       INFO : ABCDEFG"

echo "FIELD1 ; FIELD2 $RESULT ; FIELD4"
FIELD1 ; FIELD2                        INFO : ABCDEFG ; FIELD4

echo "FIELD1 ; FIELD2 ${RESULT##*( )} ; FIELD4"
FIELD1 ; FIELD2 INFO : ABCDEFG ; FIELD4


Last edited by elixir_sinari; 09-05-2012 at 08:58 AM..
# 4  
Old 09-05-2012
Ok, let me explain better this.
I have a file with some information, and I need to exctract only the INFO row, so this is the simple code:

Code:
INFO=$(grep -i INFO /info.tmp)

Now the INFO var is:
Code:
INFO="                                                INFO : ABCDEFG"

Now I need to report back this variable (WHITOUT leading spaces) into an output file with other values:

Code:
echo "$TOTAL ; $TEST ; $INFO ; $LOG"

Is there a way to do grep and remove spaces in one line?

---------- Post updated at 07:00 AM ---------- Previous update was at 06:55 AM ----------

Quote:
Originally Posted by elixir_sinari
In ksh93:
Thank elixir,
this is now my code:
Code:
INFO=$(grep -i INFO /info.tmp)
echo "FIELD1 ; FIELD2 ${INFO##*( )} ; FIELD4"

But this is the result :-(
Code:
# ./test.sh 
 ; FIELD4FIELD2                        INFO : ABCDEFG

What happens? Smilie
# 5  
Old 09-05-2012
Quote:
Originally Posted by Lord Spectre
Ok, let me explain better this.
I have a file with some information, and I need to exctract only the INFO row, so this is the simple code:

Code:
INFO=$(grep -i INFO /info.tmp)

Now the INFO var is:
Code:
INFO="                                                INFO : ABCDEFG"

Now I need to report back this variable (WHITOUT leading spaces) into an output file with other values:

Code:
echo "$TOTAL ; $TEST ; $INFO ; $LOG"

Is there a way to do grep and remove spaces in one line?

---------- Post updated at 07:00 AM ---------- Previous update was at 06:55 AM ----------



Thank elixir,
this is now my code:
Code:
INFO=$(grep -i INFO /info.tmp)
echo "FIELD1 ; FIELD2 ${INFO##*( )} ; FIELD4"

But this is the result :-(
Code:
# ./test.sh 
 ; FIELD4FIELD2                        INFO : ABCDEFG

What happens? Smilie
Try this one,

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

Cheers,
Ranga Smilie
# 6  
Old 09-05-2012
Well, this will drive me crazy:

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

Result:

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

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

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