LinuxQuestions.org
Review your favorite Linux distribution.
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 11-24-2007, 11:01 AM   #1
LMSSML
Member
 
Registered: Nov 2007
Distribution: Ubuntu Server and Desktop
Posts: 41

Rep: Reputation: 15
Smile Using linux commands


Hi there,

I'm newbie to program linux but I have the knowledge of programming.
I've tring to make something like this.

read inside a text file line by line and when the line has less than 158 characters it put's a 0 ate the end of the file of 158 characters.

...123456
...12345

after script of linux

...123456
...123450

Ive tried Wcount but it only uses to count and I've tried to make a cat but without success .

Can anyone help ?

Thanks
 
Old 11-24-2007, 11:40 AM   #2
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
I am assuming you are programming a Shell Script? It would be great if you could supply a sample of the code so we can advise.
 
Old 11-24-2007, 11:42 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
This sounds like homework, so I'm not going to write your script for you. Some pointers might help you though. These statements apply to bash and other borne-like shellsm which seems to me the most likely subject of your question (although you didn't specify a shell script):
  • If you have a variable v, you can find the length of the string assigned to v using ${#v}
  • Integer comparison is done using -lt (less than), -gt (greater than), -le (less than or equal), -ge (greater than or equal), -eq (equal to), -ne (not equal)
  • You can read from a file with the < input re-direction operator
  • You can append to a file with the >> output re-direction operator
  • You can read lines from a file with the while read variablename; do ...; done construction
  • You can print a string using the echo command
You should be able to construct the program from that information and a little searching for examples.

Last edited by matthewg42; 11-24-2007 at 11:45 AM.
 
Old 11-24-2007, 07:32 PM   #4
LMSSML
Member
 
Registered: Nov 2007
Distribution: Ubuntu Server and Desktop
Posts: 41

Original Poster
Rep: Reputation: 15
Smile Thansk for the awnsers

Hi people,

Thanks for the quick awnser and the help, I'm newbie but not lazy, I've tried to script on linux recently so I need a little help if it's possible.

Because I made the code in Pascal and it worked but if I can make it on linux its better for me.

So the could I tried it's something like this

#!/bin/sh

if [ -z $v ]; then
echo "Ficheiro:"
exit v
elif [ -f "$v" ]; then
ficheiro="$v"
fi

while read v;
do

tail -n1 readme.txt | wc -c < outline (ok it shows me the characters of first line but I want to count it per line and then make the change).

#I thought like this

done

if v -lt 159 do
#probably using dc I don't know if this is correct to increment a "0" at the end of the line.

elif -eq 159 then

fi

when using wc -c it is supposed to make a break line (per line) but the result it's something like this

wc -c readme.txt
310

So what I need it's something like this

wc -c readme.txt
1 159
2 158
3 157



Could you help me to improve with new commands to program in linux.

Thanks

Best Regards

Last edited by LMSSML; 11-24-2007 at 07:37 PM.
 
Old 11-24-2007, 08:26 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quick hint: you can post your code in [code] tags here to improve readability and preserve formatting.

You can use Pascal on Linux. I can't speak from experience, but I should imagine there would be little in the way of porting of such a program. If you're using Ubuntu, the package name is pascal-compiler.

There is no real "Linux Language" - you can find compilers/interpreters for pretty much anything which will let you build software in almost any language and have it running on Linux.

Unix-likes have a strong history of using C, shell script, awk, Perl, TCL and so on. More modern popular languages include Python, Ruby, C# and so on... Your choice of language will depend on your experience and the type of problem you wish to solve.

Shell script is very useful for cobbling together quick scripts to perform even quick complex tasks, but it is not really very good for line-by-line editing of file contents. You would usually do something like this with a specialist mini-language like sed, or something like awk or Perl.

Now, assuming you want to learn shell scripting (despite the fact that this task is probably more easily accomplished using something else), here's my suggestions about your posted script:

v will probably not be set at the start of the script, and even if it is, don't worry. When you read into it, the original value will be replaced. Don't worry about this affecting the calling shell - a child process cannot modify the environment variables in the parent script.

You while loop can accept the file in question using re-direction:
Code:
while read line; do 
   ...
done < input_file
In this loop, line contains the current line of the file. As I mentioned in the first post, ${#line} will contain the number of characters in the line... You can test this with an if statement and output the original if the length is more than 158 characters, and output the original plus a 0 if it is less.

These outputs can be done with echo, and the result appended to the output file using >>.
 
Old 11-25-2007, 06:14 AM   #6
LMSSML
Member
 
Registered: Nov 2007
Distribution: Ubuntu Server and Desktop
Posts: 41

Original Poster
Rep: Reputation: 15
Understood

Hi matthewg42,

Thanks for the help being helded.

I think I missunderstood what I've Quoted.

[Quote]v will probably not be set at the start of the script, and even if it is, don't worry. When you read into it, the original value will be replaced. Don't worry about this affecting the calling shell - a child process cannot modify the environment variables in the parent script.

You while loop can accept the file in question using re-direction:

Code:
while read line; do
...
done < input_fileIn this loop, line contains the current line of the file. As I mentioned in the first post, ${#line} will contain the number of characters in the line... You can test this with an if statement and output the original if the length is more than 158 characters, and output the original plus a 0 if it is less.

These outputs can be done with echo, and the result appended to the output file using >>.[Quote]


what I understand it's something like this:

while read line ;do

if ${#line} -lt 158 then
echo "line is above 158"
elif ${#line} -eq 159 then

exit
done < readme.txt

If it dosen't could you show me some examples to see what I've been thinking and to understood what you have said before.

Best Regards.
Thanks again for the help
 
Old 11-25-2007, 06:17 AM   #7
LMSSML
Member
 
Registered: Nov 2007
Distribution: Ubuntu Server and Desktop
Posts: 41

Original Poster
Rep: Reputation: 15
Smile misUnderstood

Hi matthewg42,

Thanks for the help being helded.
I think I missunderstood what I've Quoted.

Quote:
v will probably not be set at the start of the script, and even if it is, don't worry. When you read into it, the original value will be replaced. Don't worry about this affecting the calling shell - a child process cannot modify the environment variables in the parent script.

You while loop can accept the file in question using re-direction:

Code:
while read line; do
...
done < input_fileIn this loop, line contains the current line of the file. As I mentioned in the first post, ${#line} will contain the number of characters in the line... You can test this with an if statement and output the original if the length is more than 158 characters, and output the original plus a 0 if it is less.

These outputs can be done with echo, and the result appended to the output file using >>.


what I understand it's something like this:

while read line ;do

if ${#line} -lt 158 then
echo "line is above 158"
elif ${#line} -eq 159 then

exit
done < readme.txt

If it dosen't could you show me some examples to see what I've been thinking and to understood what you have said before.

Best Regards.
Thanks again for the help

Last edited by LMSSML; 11-25-2007 at 06:19 AM.
 
Old 11-25-2007, 07:22 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You're getting very close!

The syntax of the if statement looks like this:
Code:
if [ condition ]; then
    ...
elif [ condition ]; then
    ...
else
    ...
fi
Don't forget those square brackets. Also, note that there must be a space after the [ and before the ].
 
Old 11-25-2007, 06:34 PM   #9
LMSSML
Member
 
Registered: Nov 2007
Distribution: Ubuntu Server and Desktop
Posts: 41

Original Poster
Rep: Reputation: 15
Trying to find the end

Hi mathew

Thanks for helping me.

if I could resume the program could be something like this :

Correct me if I'm worng

#!/bin/sh

if [ -z $v ]; then
echo "Ficheiro:"
exit v
elif [ -f "$v" ]; then
ficheiro="$v"
fi

while read line v;do

if [v -lt 159]; then

echo ${#line} < v

elif [v -eq 159]; then
exit

else

echo "file is not available"

fi
 
Old 11-25-2007, 06:56 PM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
The first section doesn't accomplish anything, and is not necessary.

There's a design issue to address... You could hard-code the input file in the script and then make a while loop like I mentioned before. You could also hard-code the output file name and use appending redirects, like this:
Code:
#!/bin/bash

# remove existing output_file - we don't want to just append to existing files.
rm -f output_file

while read line; do
    if [ ${#line} -lt 158 ]; then
        echo "${line}0" >> output_file
    else
        echo "${line}" >> output_file
    fi
done < input_file
Lets say you saved this to myscript1.sh. To run it, you would make the script executable and then run it by invoking it's name (and path if your PATH environment variable doesn't contain the directory where the script is saved). i.e. this from the command line. the % represents the prompt - do not type it:
Code:
% chmod a+x myscript1.sh
% ./myscript1.sh
The chmod command changes the mode of the file (also known as the file's permissions), adding the execute permission for all users on the system (a+x). Incidentally to remove the execute permission, you would use a-x.

The program would read from the file input_file and output to the file output_file, overwriting any filee which already exists with that name.


Another approach, which is probably better style, is to let the person who runs the program do the re-direction. If you call read from a script and have not specified an input using the < input re-direction, the script will read from whatever input the caller of the script specifies with the input re-direction operator (or just the keyboard if nothing is specified).

Similarly, if you don't explicitly re-direct output to a file in the script, the person who runs the scrip can re-direct the output, deciding where it goes when they run the script.

Here's how. The script is almost the same, except the re-direction is not done:

Code:
#!/bin/bash

while read line; do
    if [ ${#line} -lt 158 ]; then
        echo "${line}0"
    else
        echo "${line}"
    fi
done
Then, when you run the script, you can re-direct the input and output like this (lets say the script is saved to the file myscript2.sh). Don't forget to change the permissions before you run it for the first time.
Code:
% chmod a+x myscript2.sh
% ./myscript2.sh < some_input_file > some_output_file
If you want to just print the output to the terminal in stead of saving it in a file, just omit the output re-direction, like this:
Code:
% ./myscript2.sh < some_input_file
 
Old 11-26-2007, 12:26 PM   #11
LMSSML
Member
 
Registered: Nov 2007
Distribution: Ubuntu Server and Desktop
Posts: 41

Original Poster
Rep: Reputation: 15
Smile Going with lot of files

Hi there Matthew

Could I have this script making for another files with different number of characters, and make cycles for one of each them.

Read in variable

compare variable

and

choose cycle (atribute cycle)



The great ideia it's something like this.

And by the way could I make the script running in windows plataform.

Thanks for the help being helded.

By the way and I'm sorry for being a boring person.

But could you explain me these I have some doubts

if [ ${#line} -lt 158 ]; then (what this means or when this is used, is this some parameter that could be exchanged ${#line}

echo "${line}0" (here I think that this line do this, put the 0 at the end of the line if it's true the above condition)
else
echo "${line}" ( this prints the line ?)
fi (the end of the cycle Probably )

Thanks

Do you have any book recomendation to begin script on linux ?

Thanks

Last edited by LMSSML; 11-26-2007 at 12:55 PM.
 
Old 11-26-2007, 03:08 PM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by LMSSML View Post
Hi there Matthew

Could I have this script making for another files with different number of characters, and make cycles for one of each them.

Read in variable

compare variable

and

choose cycle (atribute cycle)

The great ideia it's something like this.
I don't understand.

Quote:
Originally Posted by LMSSML View Post
And by the way could I make the script running in windows plataform.
Shell scripting is not a Windows technology. However, you can run shell scripts using cygwin. This is a somewhat limited solution though.

Quote:
Originally Posted by LMSSML View Post
Thanks for the help being helded.

By the way and I'm sorry for being a boring person.

But could you explain me these I have some doubts

if [ ${#line} -lt 158 ]; then (what this means or when this is used, is this some parameter that could be exchanged ${#line}
${#line} is the length of the string which is held in the variable named line.
Quote:
Originally Posted by LMSSML View Post
echo "${line}0" (here I think that this line do this, put the 0 at the end of the line if it's true the above condition)
else
echo "${line}" ( this prints the line ?)
The simplest way to take the value of the variable line is to do this:
Code:
$line
However, we want to add a '0' character immediately after this. If we do
Code:
$line0
...the shell program cannot know what we mean "the value of line followed by a 0 character" instead of "the value of the variable line0. So I used the longer variable expansion syntax:
Code:
${line}
Which makes it easy for the shell to know what I mean.

Quote:
Originally Posted by LMSSML View Post
fi (the end of the cycle Probably )
Correct.

Quote:
Originally Posted by LMSSML View Post
Thanks

Do you have any book recomendation to begin script on linux ?

Thanks
Have a look at this: http://tldp.org/LDP/abs/html/
 
Old 11-26-2007, 05:24 PM   #13
LMSSML
Member
 
Registered: Nov 2007
Distribution: Ubuntu Server and Desktop
Posts: 41

Original Poster
Rep: Reputation: 15
Cool Increment program

Quote:
Originally Posted by LMSSML
Hi there Matthew

Could I have this script making for another files with different number of characters, and make cycles for one of each them.

Read in variable

compare variable

and

choose cycle (atribute cycle)

The great ideia it's something like this.
What do I do wnat to say it's simple

If I want to increment program and have files to 58 characters I could make a cycle and solve my problem but the question in here it's the following.

The user introduces the name of the file and the name of the file it's associated to the cycle for 158 or 58 characters then he goes to the end of the file and puts a 0.

I hope I could clarify my ideias now.

and thanks once again
Best Regards.
 
Old 11-26-2007, 06:13 PM   #14
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
How would you call the program (what is your desired syntax)?
 
Old 11-27-2007, 05:57 AM   #15
LMSSML
Member
 
Registered: Nov 2007
Distribution: Ubuntu Server and Desktop
Posts: 41

Original Poster
Rep: Reputation: 15
syntax

Hi there

1.The user is prompted to insert the name of the file.
2. Program keeps the name of the file in a variable and then compares with a list of files
3. Then after comparing it calls the cycle that file belongs to.
4. cycle make the changes in the file.
 
  


Reply

Tags
awk, chmod, integer, io, linux, performance, perl, read, redirection, scripts, shell, shell script, speed, while


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
help with linux commands jld9 Linux - Newbie 7 10-23-2007 08:45 PM
Linux Commands a_newlinuxguy Linux - General 15 03-06-2007 02:53 PM
Suse Linux Commands For These Aix Commands? Vaskar Guha Linux - Software 2 12-19-2005 01:45 AM
Linux commands... hkxx9 Linux - Newbie 2 09-15-2004 09:49 AM


All times are GMT -5. The time now is 07:35 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration