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 |
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.
|
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):
|
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 |
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 These outputs can be done with echo, and the result appended to the output file using >>. |
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 |
misUnderstood
Hi matthewg42,
Thanks for the help being helded. I think I missunderstood what I've Quoted. 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 |
You're getting very close!
The syntax of the if statement looks like this: Code:
if [ condition ]; then |
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 |
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 Code:
% chmod a+x myscript1.sh 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 Code:
% chmod a+x myscript2.sh Code:
% ./myscript2.sh < some_input_file |
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 |
Quote:
Quote:
Quote:
Quote:
Code:
$line Code:
$line0 Code:
${line} Quote:
Quote:
|
Increment program
Quote:
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. |
How would you call the program (what is your desired syntax)?
|
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. |
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> " |
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 |
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 |
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.
|
awk
Hi
Thanks Mathew (again) But could awk be more quicker than my script ? Thanks |
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 you decide to go with Perl, start here: http://perldoc.perl.org/
|
All times are GMT -5. The time now is 07:15 AM. |