LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2021, 01:54 AM   #1
Ravi_Mangaluru
LQ Newbie
 
Registered: Dec 2021
Posts: 1

Rep: Reputation: Disabled
Linux Script to read a file line by line


Dear Forum Users

Season's Greetings for Ravi_Mangaluru hailing fro Mangaluru, Karnataka, India.

I need a help from you on a shell Script for the following question
I have a file called states with names of 10 states like a,b,c,d,e,f,g, etc;
My aim is to read the file states line by line and produce the output as

A beautiful State a
A beautiful State b
A beautiful State c
A beautiful State d
A beautiful State e and so on for all states in the file.

This is how my script looks
While !EOF do
echo " A beautiful Cat $state"
done

But I am not getting the desired output

Please note: This script has to be done without using BASH. It has to be written using the sh

Can someone help me in this?

Thank you.
 
Old 12-30-2021, 02:19 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,326
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Welcome.

Please edit the above post to use [code] [/code] tags around your script so that it is easier to read. Then try running it through shellcheck to proof read it for errors.

It looks like you aim to use a while loop to read line by line, so it would help to review the syntax for that.

Then give it a try again and report back on the changes. Presumably it will work, since it looks close already. If not, report on the progress and we can provide feedback.
 
Old 12-30-2021, 04:04 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
You don't have to write a script for this. A sed one-liner would be enough. See example 25. Insert five blank spaces at the beginning of each line in Sed One-Liners Explained.
 
Old 12-30-2021, 06:11 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,612

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553

https://mywiki.wooledge.org/BashFAQ/001

 
Old 12-30-2021, 07:18 AM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,140
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
Code:
while read line; do echo "$line"; sleep 1; done < file.txt

cat file.txt | while read x; do echo "$x"; sleep 1; done
 
Old 12-31-2021, 05:57 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
"A beautiful Cat"
 
Old 12-31-2021, 10:22 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,655

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by Ravi_Mangaluru View Post
Dear Forum Users
Season's Greetings for Ravi_Mangaluru hailing fro Mangaluru, Karnataka, India.

I need a help from you on a shell Script for the following question I have a file called states with names of 10 states like a,b,c,d,e,f,g, etc; My aim is to read the file states line by line and produce the output as
Code:
A beautiful State a
A beautiful State b
A beautiful State c
A beautiful State d
A beautiful State e and so on for all states in the file.
This is how my script looks
Code:
        While !EOF do
            echo " A beautiful Cat $state"
        done
But I am not getting the desired output Please note: This script has to be done without using BASH. It has to be written using the sh Can someone help me in this?
Why don't you tell us what you ARE getting when you run your script, and how you're running it. There is a site call shellcheck.net that will point out some flaws in your script, but this sounds very much like a verbatim-homework question. And using capital letters for While and Cat won't get you far.
 
Old 01-05-2022, 10:46 PM   #8
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
@Ravi_Mangaluru

You can try the following
Code:
$ cat ./4175705567.sh
#!/bin/sh
/usr/bin/awk -F',' '{
 for(col=1;col<NF;col++)
        printf( "A beautiful State %s\n", $col);
 printf( "A beautiful State %s\n", $1);
}' 4175705567.txt
$ chmod +x ./4175705567.sh
$ ./4175705567.sh
A beautiful State a
A beautiful State b
A beautiful State c
A beautiful State d
A beautiful State e
A beautiful State f
A beautiful State g
A beautiful State a
$ /usr/bin/awk -F',' '{
 for(col=1;col<NF;col++)
        printf( "A beautiful State %s\n", $col);
 printf( "A beautiful State %s\n", $1);
}' ./4175705567.txt
A beautiful State a
A beautiful State b
A beautiful State c
A beautiful State d
A beautiful State e
A beautiful State f
A beautiful State g
A beautiful State a
$ #Updated script. Character "a" was repeated in your input file. Handled that duplicate errors.
$ cat ./4175705567.sh
#!/bin/sh
if [ -f ./4175705567.txt ]
then
 /usr/bin/awk -F',' '{
        for(indx=1;indx<=NF;indx++)
        {
                if ( "" != $indx )
                {
                        BeautifulState[$indx] = $indx;
                }
        }
 }
 END {
        for(DispVal in BeautifulState)
        {
                printf( "A beautifule State %s\n", DispVal);
        }
 }' ./4175705567.txt
else
 echo ls ./4175705567.txt
 ls ./4175705567.txt
fi
$ ./4175705567.sh
A beautifule State a
A beautifule State b
A beautifule State c
A beautifule State d
A beautifule State e
A beautifule State f
A beautifule State g
$ awk -F',' '{
        for(indx=1;indx<=NF;indx++)
        {
                if ( "" != $indx )
                {
                        BeautifulState[$indx] = $indx;
                }
        }
 }
 END {
        for(DispVal in BeautifulState)
        {
                printf( "A beautifule State %s\n", DispVal);
        }
 }' ./4175705567.txt
A beautifule State a
A beautifule State b
A beautifule State c
A beautifule State d
A beautifule State e
A beautifule State f
A beautifule State g
$
 
  


Reply

Tags
sed



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
read line by line a text file and define a file from each file yasmine Linux - Newbie 5 12-10-2012 04:53 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
Script to read line by line from a file kushalkoolwal Programming 20 01-27-2006 04:17 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:36 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