Hi MadeInGermany,
The sort man page description of sort keys (whether using -k keydef or +key_start -key_end) and of the options/flags is complicated because there are so many special cases in the way sort keys are specified. If you look at lines as C arrays of type char where the index of the first character in the array is 0, the old way of specifying key definitions will feel "right" to you. If you look at the way most other UNIX utilities number fields and characters within fields (e.g., awk, cut, fold, head, paste, and tail), the -k keydef key definitions will feel "right" to you. I can translate between the two, but I generally find I'm more likely to get the new form correct the first time. And, I find it much easier to explain the new form to a UNIX utility newby whether or not they are experienced C programmers. Most of the people that I know who feel more comfortable with the old form learned to use the old form before the late 1980's when the new form was invented.
The options -b, -d, -f, -i, -n and -r can be given as options that apply to all sort keys that do not include any flags or as flags that only apply to the key to which they are attached. The -t char option can only be specified as an option; not as a flag.
When used as a flag, b applies only to the start field or end field specification to which it is attached. All other flags can be applied to the start field specification, to the end field specification, or both and have the same effect.
If a key field definition has any flags attached, those flags override ALL options except -t char. So, for example, if I want to sort in reverse (decreasing) numeric order on unit price and on reverse case-insensitive alphabetic order by ingredient name within groups of identical unit prices I could use any of the following:
but the following won't work:
because the n flag on the first key specification overrides both the -r and -f options.
The main difference between the two forms is that field numbers and characters in fields are numbered from 1 when using -k keydef, but are numbered from 0 when using +key_start -key_end. For example, to sort alphabetically on the 7th character of the first field skipping over leading spaces in the sample data shown in post #1, the following four commands all specify the same sort key:
and all produce the output (note that there are two <space>s between the line number and the ingredient name in the 1st field):
In the most general -k keydef option form:
if the optional .first_character_number is omitted, it defaults to the first character in the given field. If the optional .last_character_number is omitted, it defaults to the last character in the given field. If the entire optional end field number specification (,field_end_number[.last_character_number][flag...]) is omitted, it defaults to the end of the current line.
An equivalent sort key using the old style key definition for the above -k option is:
where all four occurrences of -1 are numeric calculations on the previous number; not literal strings.
I hope this helps. And, I really hope I don't have any typos in this post that further confuse any readers trying to figure out how sort works.
Last edited by Don Cragun; 12-04-2017 at 07:04 AM..
Reason: Fix intro.
I'm trying to read a bunch of log files and output the lines that contain particular strings.
To accomplish this, I've been running the following from the command line:
find . -name "*" | xargs grep " " | grep " " > output.txt
Two grep statements are needed in case I'm looking for a... (3 Replies)
i want to search in the current directory all the files that contain one word for example "hello"
i want to achieve it with the grep command but not with the grep * (2 Replies)
Hello.
I would like to ask your help regarding the cp command. We are using a cp command to create a back-up copy of our file but to no avail. It's just not working. We already checked the file and directory permissions and all seems correct.
We have a script (ftp.script) which calls on... (1 Reply)
Hi All,
Is there any way where we can run few commands with different shell in a shell script ?
Let's have an example below,
My first line in script reads below,
#!/bin/sh
However due to some limitation of "/bin/sh" shell I wanted to use "/bin/bash" while executing few... (9 Replies)
I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found"
My code is as follows:
--------------------------
#!/bin/sh
user="test"
DIR="/bldtmp/"$user
VIEW="test.view1"
echo "TMPDIR before export... (4 Replies)
Hi All,
I have a question related to Shell scripting. In my shell script, I have following two commands in sequence:
sed 's/^/grep "^120" /g' $ORIGCHARGEDAMTLIST|sed "s;$;| cut -f$FIELD_NO1 -d '|' | awk '{ sum+=\$1} END {printf (\"%0.2f\\\n\", sum/100)}' >$TEMPFILE
mv $TEMPFILE $ORIGFILE... (3 Replies)
Discussion started by: angshuman
3 Replies
7. Post Here to Contact Site Administrators and Moderators
Variable I have in my shell script
diff=$1$2.diff
id=$2
new=new_$diff
echo "My id is $1"
echo "I want to sync for user account $id"
##awk command I am using is as below
cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new
I could see value of $id is not passing to the awk... (0 Replies)
I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist.
diff=$1$2.diff
id=$2 new=new_$diff
echo "My id is $1"
echo "I want to sync for user account $id"
##awk command I am using is as below
cat $diff | awk... (2 Replies)
I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist.
diff=$1$2.diff
id=$2 new=new_$diff
echo "My id is $1"
echo "I want to sync for user account $id"
##awk command I am using is as below
cat $diff |... (1 Reply)
Hi everyone
I have a problem with my script
If I try directly this command
/usr/bin/nice -n 19 mysqldump -u root --password="******" wiki_schneider -c | nice -n 19 gzip -9 > /point_de_montage/$(date '+%Y%m%d')-wiki-db.sql.gz
It works
But if I simply add this command in a script and... (8 Replies)