LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using linux commands (https://www.linuxquestions.org/questions/linux-newbie-8/using-linux-commands-602094/)

LMSSML 11-24-2007 10:01 AM

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

harry edwards 11-24-2007 10:40 AM

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.

matthewg42 11-24-2007 10:42 AM

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.

LMSSML 11-24-2007 06:32 PM

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

matthewg42 11-24-2007 07:26 PM

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 >>.

LMSSML 11-25-2007 05:14 AM

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

LMSSML 11-25-2007 05:17 AM

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

matthewg42 11-25-2007 06:22 AM

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 ].

LMSSML 11-25-2007 05:34 PM

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

matthewg42 11-25-2007 05:56 PM

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

LMSSML 11-26-2007 11:26 AM

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

matthewg42 11-26-2007 02:08 PM

Quote:

Originally Posted by LMSSML (Post 2971278)
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 (Post 2971278)
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 (Post 2971278)
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 (Post 2971278)
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 (Post 2971278)
fi (the end of the cycle Probably :) )

Correct.

Quote:

Originally Posted by LMSSML (Post 2971278)
Thanks

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

Thanks

Have a look at this: http://tldp.org/LDP/abs/html/

LMSSML 11-26-2007 04:24 PM

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.

matthewg42 11-26-2007 05:13 PM

How would you call the program (what is your desired syntax)?

LMSSML 11-27-2007 04:57 AM

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.

matthewg42 11-27-2007 05:15 AM

In part 2 what do you mean "compares"? That suggests that some operation should be performed if some condition is true, but you do not say what.

In general, you can use a while read loop to take input from the keyboard:
Code:

echo -n "Enter a file name> "
while read filename; do
    if [ "$filename" = "" ]; then
        echo "Nothing entered.  I will not ask again"
        break
    fi
    echo "I got this: $filename"
    echo -n "Enter another file name> "
done


LMSSML 11-28-2007 12:20 PM

sorry for my late explanation (LOL)
 
Hi mathew,


What I want to says with "compares" in a short example it's something like this.

Input from user

tabela2.txt

v:=read (file)

the file will be stored in v so now we could make the "compare"

z is a list where inside has something like this:

tabela1.txt 158 (characaters)
tabela2.txt 38 (characters)
tabela3.txt 45 (characters)

if v exist in z then

(it goes to the 8th cycle) and do something like this.
:8

while read line; do
if [ ${#line} -lt 38 ]; then
echo "${line}0"
else
echo "${line}"
fi
done

but if the file it's tabela3.txt then he goes to

:7
while read line; do
if [ ${#line} -lt 45]; then
echo "${line}0"
else
echo "${line}"
fi
done

Thanks

LMSSML 12-02-2007 04:53 AM

Speed up to analyse
 
Hi there !!

Is any way to accelarate scripts on Linux

I've got files with 1 million (near of 2 million) of lines and to analyse 12 files it takes 2 days if I have 70 files it will be longer with the time.

Is there anyway to accelarate this situation ?

Thanks

best Rgeards

matthewg42 12-02-2007 07:20 AM

Shell scripts are really a quick and quite slow glue for combining the functionality of other programs. As I told you in post #5 of this thread, they are not good for line-by-line editing of large sets of data. You should consider writing your program in a language more suited to the task, such as Perl or Python.

LMSSML 12-04-2007 05:50 AM

awk
 
Hi

Thanks Mathew (again)

But could awk be more quicker than my script ?

Thanks

matthewg42 12-04-2007 06:29 AM

For sure awk will be a lot faster than a shell script in this case. Perl will probably be even faster, although it's more to learn than awk, so maybe awk would be easier to get started with.

For example, I created a large test file (15 meg / 100000 lines) and ran the shell script on it. The time used by the process was over 31 seconds. When I did the same thing with an awk program it took just 1.7 seconds. A similar program written in Perl took less than half a second.

The text processing part of the program in awk looks like this:
Code:

{
        if ( length($0) < 38 ) {
                print $0 "0";
        }
        else {
                print $0;
        }
}


chrism01 12-04-2007 05:26 PM

If you decide to go with Perl, start here: http://perldoc.perl.org/


All times are GMT -5. The time now is 07:15 AM.