LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2018, 06:56 PM   #1
tempuser123
LQ Newbie
 
Registered: Oct 2018
Posts: 5

Rep: Reputation: Disabled
Need help looping through a file and outputting each line in the file plus the output from curl - bash script


Hello everyone!

I am having some trouble with a bash script. basically what I am trying to do is loop through a file (input.list) line by line and have it run a curl command against a website. I have it doing that right now, and it is outputting the data I want it to, but i also want it to display what is in the orinal file in the first column. I've listed some examples below.


Key
- script.sh : the script i am working on
- input.list : the input file
- output.list : the file i want it to output to

script.sh
Code:
#!/bin/bash

while read line; do
  echo "second column"
done < "input-test.list" > "output-test.list"
input.list
Code:
A
B
C
D
Output I am getting
Code:
second column
second column
second column
second column
Output I want to go into output.list
Code:
A: second column
B: second column
C: second column
D: second column
NOTE: I replaced the curl command with a quick 'echo' command for simplicity. but the 'second column' will be a random value that is parsed from the actual curl command that will be given.

If anyone could help me out that would be great! Thanks in advance!
 
Old 10-31-2018, 07:31 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
From your description I think something like this should do the trick:

Code:
echo "${line}: ${second_column}"
...where ${second_column} is the data parsed from curl.
 
Old 10-31-2018, 07:33 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
You need to include the contents of the variable line in your output. Does that hint help?

Oh. astrogeek showed you how...was this homework?

Last edited by scasey; 10-31-2018 at 07:34 PM.
 
Old 11-04-2018, 12:53 AM   #4
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
Sample code for the same:
Code:
$ /bin/cat ./Looping_through_file.sh
#!/bin/bash
if [ ! -f "./input-test.list" ]
then
        echo "/bin/ls ./input-test.list"
        /bin/ls "./input-test.list"
else
        while read CURR_LINE
        do
                second_column=$(/usr/bin/head -1 /dev/urandom | /usr/bin/cksum | /bin/sed "s/ .*//;" |\
                /usr/bin/awk '{ printf( "%010d", $0); }')
                echo "$CURR_LINE: ${second_column}"
        done < "./input-test.list"
fi
$ ./Looping_through_file.sh
/bin/ls ./input-test.list
/bin/ls: cannot access './input-test.list': No such file or directory
$ echo -e "A\nB\nC\nD" > "./input-test.list"
$ ./Looping_through_file.sh
A: 2982344826
B: 0116823401
C: 0076856536
D: 2648229816
$
 
  


Reply



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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 AM.

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