Extract data from a file and store it in a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract data from a file and store it in a variable
# 1  
Old 02-28-2014
[Solved] Extract data from a file and store it in a variable

Hi ,

I have an file like below ,

Code:
cat input.txt
'Pattern2' => 'blahdalskdahdlahldahdlakhdlahdlkajdlkaadasdadadadadadadasda
ajlalnalndklandlaksdlkaddd'
'Pattern2' => 'aohaonalkndlanldandlandklasnldnaldnak'
............
........
.....

Here is what am trying to do ,

I want to grep for all the pattern2 in a file and i need the content in single quote of that to be stored in a variable which i want to use later.
I tried to find a way alot ,but not able to get it.

Please help me in achieving this.

Last edited by Corona688; 02-28-2014 at 02:00 PM..
# 2  
Old 02-28-2014
A lot of people hate xargs because of the way it unparses single quotes... I actually find it useful for that in situations like this.

1) Convert newlines into spaces.
2) feed into xargs. It will print arguments separately, and intelligently use quotes for grouping before stripping them out.
3) Read into shell, one line at a time, using => arguments to detect when data's coming.

Code:
PAT="Pattern2"
OUT=""
# Strip out newlines, parse 'quoted strings' into single arguments,
# print arguments 1 at a time and save to file.
tr '\n' ' ' < /inputfile | xargs -n 1 > /tmp/$$

while read LINE
do
        if [ "$LINE" == "=>" ]
        then
                read LINE
                [ "$TITLE" = "$PAT" ] && OUT="$OUT$LINE"
                continue
        fi

        TITLE="$LINE"
done < /tmp/$$

rm -f /tmp/$$

echo "Got data $OUT"

# 3  
Old 02-28-2014
Hi,
I am new to shell scripting and trying to solve things. As per your question, if your content is in file Pattern.txt, then your output with concatenating string value will be in Result.txt as per my below script:


Code:
#!/bin/sh


cp  Pattern.txt test2.txt
chmod 777 test2.txt
sed "s/'//g" test1.txt > test2.txt

cp test2.txt test3.txt
chmod 777 test3.txt
sed "s/ //g" test2.txt > test3.txt
cp test3.txt test1.txt

grep "Pattern2=>" test1.txt | cut -f 2 -d">" | tr -d '\n' >> Result.txt

rm test2.txt test3.txt

# 4  
Old 03-03-2014
thanks alot Corona688 for your response.
 
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 store file name in a variable?

Hi All, Daily I am generating a file dfm_daily_file_ ex: dfm_daily_file_05072015 date will be changed daily. Once the file is FTP it is deleted. I have tried the below code to get the file name with any date and store it in a variable its not working. #!/bin/ksh ... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

2. Shell Programming and Scripting

Extract the data from tag below asssigned the variable

Sample data file. UID=12_C_S_12_PrecisionMktg^12_C_S_12_PrecisionMktg_LinkList NameField=LINK_NAME ExternalTrackingField=EXTERNAL_TRACKING CategoryField=LINK_CATEGORY URLField=LINK_URL UID=12_PrecisionMktg^FILTER_12_C_S_12_PrecisionMktg comma=,... (5 Replies)
Discussion started by: bmk
5 Replies

3. Shell Programming and Scripting

Extract a string from a file and store it in variable

Hi, I've a file ImageSizeDetails.txt with the following contents: Image Name: swncd 01.10.00.04 Created: Wed Jan 9 14:05:48 2013 Image Type: ARM Linux Multi-File Image (gzip compressed) Data Size: 7351011 Bytes = 7178.72 kB = 7.01 MB Load Address: 00008000 Entry Point: ... (6 Replies)
Discussion started by: Parameswaran
6 Replies

4. Shell Programming and Scripting

Store Large data in Variable in csh

Hi All, This is my first post!! :) How to store huge data in a single variable in csh. Right now i can store upto ~700kb of data. I still want to store more data and i dont want to use perl. Is there any method to store like that...Please let me know.. Thanks (3 Replies)
Discussion started by: vickra
3 Replies

5. Programming

How to extract data from CVS log files and store it in database ?

Am currently working on CVS projects .. I have generated the cvs log file which is in the RCS file format . .I want to extract file path ,total revision ,date ,author and message from that file . .I want a program in java which would extract the data from cvs log file. .Pls help me out.. My... (0 Replies)
Discussion started by: EVERSOFT
0 Replies

6. UNIX and Linux Applications

Retrieving data from a database and store to a file

Hi I'm using and Oracle 10g Database. I want to write a script to retrieve data from the database and store it toa file. I'm using simple sql statements such as Select * from celltable I don't know how to specify the database name etc. I have this but it doesn't work ... (1 Reply)
Discussion started by: ladyAnne
1 Replies

7. Shell Programming and Scripting

store the table data in excel file

Hello - I have a below table and i want to extract the data into excel sheet and send to different location. Here is the table structure... SQL> desc t_i1_exportdocs Name Null? Type ----------------------------------------- --------... (11 Replies)
Discussion started by: govindts
11 Replies

8. UNIX for Dummies Questions & Answers

Using GREP/AWK to extract a number and store it as a variable

Hello, I have a question regarding the awk command. Here is the line I need to grep: 1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157 Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any... (3 Replies)
Discussion started by: modey3
3 Replies

9. Shell Programming and Scripting

store the first line of a file in a variable

i have to store the files in a folder and assign a variable to the the files. (0 Replies)
Discussion started by: dineshr85
0 Replies

10. Shell Programming and Scripting

How to store Data in a File

I want to read a data from a read command and store it in a file...... Plz send me how I can do that Ex: read val and I want to store this val in a file called temp. (2 Replies)
Discussion started by: krishna_sicsr
2 Replies
Login or Register to Ask a Question