Sponsored Content
Homework and Emergencies Homework & Coursework Questions String editing using sed? awk? Post 302370905 by bakunin on Thursday 12th of November 2009 09:32:14 PM
Old 11-12-2009
Quote:
Originally Posted by peage1475
from my understanding cmp -s will produce either a 0, if the files are identical, or a 1 if the files are different. So my if statment says that if the files are different add $path to files, else prin working. why is this only producing working... when I know for a fact that there are at least 5 that are modified different then the one it is comparing too?
The reason is you have a simple syntactic error, but lets first go over what you have done correctly, because there are some good points.

First, you have found a fundamental structure in Unix: you can use the return code of a program to determine how it has operated. In this case you - correctly - try to use the return code of "cmp". Very good.

Second, you have found out another fundamental thing: like in the C programming language throughout Unix a value of 0 is considered to be a logical TRUE while every other value is considered to mean a logical FALSE.

Now to your error: the correct syntax of the if-statement is:

Code:
if [ :TRUE: ] ; then
     some_commands
else
     some_other_commands
fi

Consider ":TRUE:" to be some process (or set of processes in a pipeline) or condition which either results in a "0" (TRUE) or another integer (FALSE).

In the historical Unix there was a command called "[", which was a link to the command "/usr/bin/test", which is still there. I suggest you read to man page of "test" to find out which possibilities it has. Basically "test" gets some logical expression and like any other program returns "0" (if the logical expression results in TRUE) or "1" (FALSE).

Because "[" was a normal program (most shells have implemented it as an internal command in the meantime, but Unix is adamant about being backwards compatible) it is clear why before and after it there have to be spaces: the rest of the line up to "]" are commandline arguments to "test" and therefore there have to be spaces like "ls-l" is wrong and "ls -l" is right.

Try the following two scripts:

Code:
#!/bin/ksh
if 0 ; then
     echo TRUE
else
     echo FALSE
fi

#!/bin/ksh
if [ 1 = 1 ] ; then
     echo TRUE
else
     echo FALSE
fi

In the first script then change the "0" to "1" or another integer and try again - notice what happens in the light of what i did say.

In the second script change the (obviously true) expression to another expression and experiment. Watch the output.

Bonus question: the expression "1 = 1" is working, but in fact the expression is not suited for comparing integer values ("=" is for comparing strings). How should it be phrased as true integer comparison? Hint: consult the man page!

You should now be able to change your script to work. That leaves two questions:

1) You now feed some file content to the while-loop, but this is hardly an up-to-date user directory. It is a good development device, though, so i am very satisfied that you had this idea to "simulate having this list" for the moment. It is a clever way of solving one problem after the other - i do exactly the same when writing scripts. Still, you have to solve the problem of "how to obtain a list of all defined users in a machine" and - "how to get the subset of this list i am interested in"? Any ideas?

2) Notice, that you output only "$path" to an output file, and not, what you want to stand there. If you prepare the output file this way it will be intermediary in nature and this is not, what you want. Is there a way to manipulate strings in the shell and - if this is possible - which commands might be useful for this purpose? Hint: consult your classes written material, the recommended book or whatever you got in this class rather than the "man-page" first - this is something the man-pages are poorly suited to research. Once you have a good idea of string-manipulating commands read their man pages of course.

All in all I'm quite satisfied with your progress so far - you might not know it but you are close to the solution. Keep up the good work.

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Editing File using awk/sed

Hello Awk Gurus, Can anyone of you help me with the below problem. I have got a file having data in below format pmFaultyTransportBlocks ----------------------- 9842993 pmFrmNoOfDiscRachFrames ----------------------- NULL pmNoRecRandomAccSuccess -----------------------... (4 Replies)
Discussion started by: Mohammed
4 Replies

2. Shell Programming and Scripting

awk/sed - getting string instead of number

Hi! I am writing a script handling downloading list of files and I have to check whether file is present locally and if not finished than continue downloading. To do so I have to compare sizes of remote file and local file. To check remote file size I have to parse something like this: ... (2 Replies)
Discussion started by: hrwath
2 Replies

3. Shell Programming and Scripting

sed or awk editing help

Hi all I use aix (sadly). I've got a file consisting of fields separated by commas, I need a sed or awk command that will delete all spaces between two commas as long as there are only spaces between the commas. eg ,abc, ,sd , ,dr at would become ,abc,,sd ,,dr at I have... (53 Replies)
Discussion started by: mychmose
53 Replies

4. Shell Programming and Scripting

Line/Variable Editing for Awk sed Cut

Hello, i have a file, i open the file and read the line, i want to get the first item in the csv file and also teh third+6 item and wirte it to a new csv file. only problem is that using echo it takes TOO LONG: please help a newbie. below is my code: WorkingDir=$1 FileName=`cut -d ',' -f... (2 Replies)
Discussion started by: limamichelle
2 Replies

5. Shell Programming and Scripting

match string exactly with awk/sed

Hi all, I have a list that I would like to parse with awk/sed. The list is contains entries such as: JournalTitle: Biochemistry JournalTitle: Biochemistry and cell biology = Biochimie et biologie cellulaire JournalTitle: Biochemistry and experimental biology JournalTitle: Biochemistry and... (6 Replies)
Discussion started by: euval
6 Replies

6. Shell Programming and Scripting

editing file with awk cut and sed

HI All, I am new to unix. I have a file would like to do some editing by using awk, cut and sed. Could anyone help? This file contain 100 lines. There are one line for example: 2,"102343454",5060,"579668","579668","579668","SIP",,,"825922","035885221283026",1,268,"00:59:00.782 APR 17... (2 Replies)
Discussion started by: mimilaw
2 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. Shell Programming and Scripting

sed awk to remove the , in a string

Dear All, Can anyone help to remove the , bewteen "" in a string by using sed or awk? e.g. input : 1,4,5,"abcdef","we,are,here",4,"help hep" output:1,4,5,"abcdef","wearehere",4,"help hep" Thanks, Mimi (5 Replies)
Discussion started by: mimilaw
5 Replies

9. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

10. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies
All times are GMT -4. The time now is 07:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy