LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-10-2017, 01:11 PM   #1
glennbtn
Member
 
Registered: Dec 2009
Posts: 125

Rep: Reputation: 18
Getting data out of text file with bash script


Hi All

I have a text file on the server which changes daily. The contents are listed on 2 lines 1 with a user id and 1 with an amount as below

id:1
amount:5

I need to be able to read the figures only in to variables and then run the curl script I have inserting these 2 variable in to the line of code.

Can anyone help with how I would do this

Thanks

Glenn
 
Old 05-10-2017, 02:03 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,425

Rep: Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054
sed ?
Code:
id=`sed 's/id://' textfile`  
amt=`sed 's/amount://' textfile`
...then $id and $amt will contain the values you want

Sean
 
Old 05-10-2017, 02:57 PM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917
Quote:
Originally Posted by scasey View Post
sed ?
Code:
id=`sed 's/id://' textfile`  
amt=`sed 's/amount://' textfile`
...then $id and $amt will contain the values you want

Sean
Hi Sean.

Did you test this code before suggesting it? Give it a go...
 
Old 05-10-2017, 06:17 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,706

Rep: Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548
And beyond that, is it guaranteed to be only one occurrence ?. And if not do you always want the first/last/other ... ?
 
Old 05-10-2017, 07:10 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Mod:

Ok, you may like this one.

Code:
#!/bin/bash
#gets matched colon(s)
how1=$(grep ":" "$1")
#1st instance
ans1=${how1#*:}
#second instance
ans2=${how1##*:}

echo "ans 1. $ans1"
echo "ans 2. $ans2"
testfile
Code:
testfile
one:thisisone
two:thisistwo
three there
results
Code:
userx%slackwhere ⚡ scripts ⚡> ./grepgrab testfile
ans 1. thisisone
two:thisistwo
ans 2. thisistwo
userx%slackwhere ⚡ scripts ⚡>
I withdraw -- I just noticed that cure script not bash script.

Last edited by BW-userx; 05-10-2017 at 08:11 PM.
 
Old 05-10-2017, 08:05 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,425

Rep: Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054
Quote:
Originally Posted by hydrurga View Post
Hi Sean.

Did you test this code before suggesting it? Give it a go...
Yes, and no. I tested the concept on a single line file. As you so gently pointed out, that wasn't a thorough enough test.
Using the file the OP presented, this works (but it's kind of klunky), and assumes that the file is always as he showed it; with one line as id:number and another line with amount:number

Code:
#!/bin/bash
# test.sh

# select the id line and parse it
id=`grep id textfile | sed 's/id://'`
# select the amount line and parse it
amt=`grep amount textfile | sed 's/amount://'`

# use the values in the curl script...
echo 'id is '$id
echo 'amount is '$amt
Code:
./test.sh
id is 1
amount is 5

Last edited by scasey; 05-10-2017 at 08:16 PM. Reason: clarification
 
Old 05-10-2017, 08:22 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,706

Rep: Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548
I think hydrurga was referring the (likely) presence of other data. sed is more than capable of doing the job.
But my questions to the OP remain.
 
Old 05-10-2017, 09:10 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,425

Rep: Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054
Quote:
Originally Posted by syg00 View Post
I think hydrurga was referring the (likely) presence of other data. sed is more than capable of doing the job.
syg00, You are (also) too kind...my initial "solution" was not correct/adequate/sufficient. As you can see, I'm a noob here, and am working to fit in while providing assistance where I can.

Sean

Last edited by scasey; 05-10-2017 at 09:14 PM.
 
Old 05-10-2017, 09:23 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,706

Rep: Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548
sed has a lot more flexibility than simple replacement - although it can be obtuse.
Have a read of this to see how lines can be filtered.
 
1 members found this post helpful.
Old 05-10-2017, 10:38 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,862

Rep: Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598
Yet another way...
Code:
id=$(grep id t.t|cut -d':' -f2)
amount=$(grep amount t.t|cut -d':' -f2)
 
Old 05-11-2017, 03:18 AM   #11
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917
Quote:
Originally Posted by syg00 View Post
I think hydrurga was referring the (likely) presence of other data. sed is more than capable of doing the job.
But my questions to the OP remain.
Sorry for the confusion. My comment above was targeted at the fact that Sean's initial solution did not work for the two-line input file that was part of the OP's original specification.

As it is, we'll probably need feedback from OP before being able to go any further with this.
 
Old 05-12-2017, 02:12 AM   #12
glennbtn
Member
 
Registered: Dec 2009
Posts: 125

Original Poster
Rep: Reputation: 18
Thanks for the comments here

I did try the initial commands and noticed that only the 1st variable worked and nothing for the 2nd.

I will give the other suggestions a go

Many thanks

Glenn
 
Old 05-12-2017, 02:18 AM   #13
glennbtn
Member
 
Registered: Dec 2009
Posts: 125

Original Poster
Rep: Reputation: 18
Sorry yes just once and then the text file will be deleted and a new 1 created with new data
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read text files and manipulate data for a single output file - hopefully bash uzee Programming 7 01-10-2015 08:34 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 11:40 AM
Bash Scripting help, altering data in text file. trey85stang Linux - General 4 05-31-2006 05:18 AM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration