I have a script that writes parameters in alphabetic order.
But I have a parameter which have 3 lines. There is no continuation character ( '\' ). Each of the three lines finish with 'cr'. But line 2 and 3 of the concerning parameter start with a tab char (but should be one or more space).
When my script finished, I got this :
The position of the 3 red lines are arbitrary.
Now I would like now to re-read the result file and modify it to obtain this :
Any help is welcome.
Is it about sorting of arguments that are given to a script or a script function?
The sorting of arguments requires an explicit sorting, either with an external sort command or with a sort function.
A newline is an obstacle for the external sort command, unless sort understands -t $'\0' or similar tricks.
A shell function can do it, as shown by the following sample:
Proper quoting of variables in command arguments is important! Only then all special characters are treated normal, even newline characters.
--
You haven't provided your shell script yet. So maybe my assumptions were totally wrong...
--
You haven't provided your shell script yet. So maybe my assumptions were totally wrong...
The file to be re-aranged has been posted.
---------- Post updated at 16:34 ---------- Previous update was at 16:17 ----------
@Madeingermany
@Rudic
Took initial file.
Remove comments.
remove blank lines.
sort the file.
Add my header.
That give the file I just provide.
In that file :
line 28 must be the first position of the three (new position)
line 54 must follow in second position (new position)
line 27 must the last in third position (new position)
But the lines position are unknown.
Only the contents of the offending lines are known.
If you need more details then asks.
thank you for helping.
---------- Post updated at 16:49 ---------- Previous update was at 16:34 ----------
Should be something like :
Code:
Cut line with contents = 'ddd $daemon_directory/$process_name $process_id & sleep 5'
Find line with contents = 'debugger_command ='
Paste the line after the find what one have cut before
Cut the line contents = 'PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin'
Find line with contents = 'debugger_command ='
Paste the line after the find what one have cut before
PS I know nothing about awk, and very few about sed.
In post #1 you said that your input lines are terminated with 'CR', which I assume you meant to be a <carriage-return> character. But the sample file you uploaded (main.cf.txt) does not contain any <carriage-return> characters; instead it contains normal UNIX text files with lines terminated by <newline> characters.
In post #1 you showed us sample input data containing multiple adjacent lines starting with <tab>. In the file you uploaded, that never happens, but there are multiple occurrences of configuration lines followed by <tab>bed lines.
In post #1 you didn't mention anything about comment lines nor about empty lines that are not supposed to be sorted, but there are lines at the start of the file you uploaded that I assume are not intended to be sorted. (But, of course, that is a wild assumption because you have not really shown us what output should be produced by processing the file you uploaded.)
You also mention in post #1:
Quote:
But line 2 and 3 of the concerning parameter start with a tab char (but should be one or more space).
but there aren't any <tab> characters in your example in post #1 and there aren't any leading <space> characters in the file you uploaded.
Assuming that you want all lines starting with a "#" and all blank lines to appear unsorted at the start of the output file produced (in the order in which they were found in the input) and that the first lines of configuration data are to be sorted into alphanumeric order with continuation lines (any line that contains at least one non-blank character but with a first character that is a <space> or a <tab>) unsorted and attached to the configuration line it follows, the following bash script using awk and sort to preprocess data to be sorted, sort to sort the configuration data, and awk to post process the sorted data seems to do what I'm guessing you want:
Code:
#!/bin/bash
# Create a name for hte temporary output file to be used by sort.
sorted_cf="sorted.cf."$$
# Use awk to preprocess your input file:
# directly printing comments and blank lines.
# using sort to sort configuaration data, and
# postprocess the sorted configuration data to print the sorted
# configuratoin data.
awk -v sorted_file="$sorted_cf" '
BEGIN { # Set input and output field separators.
FS = OFS = "\r"
# Set sort command to be used to sort confiiguration data (not comments).
sort = "sort -t\"\r\" -k1,1 -k2,2n -k3,3n -o " sorted_file
}
/^#/ || /^[ \t]*$/ {
# Print comments and blank lines at start of output.
print
next
}
/^[ \t]/ {
# Filter non-blank lines starting with a blank by creating a line to be
# sorted on the previous line not starting with a blank, a count of the
# number of times that line has been seen, and a count of the lines seen
# since that line was. Also print (but do not sort) the contents of
# this line.
print last, n[last], ++seq, $0 | sort
next
}
{ # Filter non-blank lines starting with a non-blank by creating a line to
# be sorted on the full contents of this line, the number of times this
# line has been seen, and 1 (the number of lines in this set so far).
print last = $0, ++n[$0], seq = 1 | sort
}
END { # Close the output to be filtered through the sort utility.
close(sort)
# And read back i nthe sorted configuration data...
while ((getline < sorted_file) == 1) {
if(NF == 3)
# For lines containing initial lines and counts, print
# just the initial line data.
print $1
else if(NF == 4)
# For lines containing initial lines, counts, and a
# continuation line, print just the continuation line.
print $4
}
}' main.cf.txt # close the awk script and name the file to be processed.
# Remove the temporary output file produced by sort.
rm -f "$sorted_cf"
With the sample data you uploaded, this produces the output:
If this isn't what you're trying to do, please specify much more clearly exactly what it is that you're trying to do and show us the output you are trying to produce.
In post #1 you said that your input lines are terminated with 'CR', which I assume you meant to be a <carriage-return> character. But the sample file you uploaded (main.cf.txt) does not contain any <carriage-return> characters; instead it contains normal UNIX text files with lines terminated by <newline> characters.
Sorry I would say <newline>
Quote:
Originally Posted by Don Cragun
In post #1 you showed us sample input data containing multiple adjacent lines starting with <tab>. In the file you uploaded, that never happens, but there are multiple occurrences of configuration lines followed by <tab>bed lines.
The post #1 is my question.
The first block of code show the source file part where I have a problem.
The second bloc of code show the result of my script with the offending lines.
The third block (last block) show the result I need to get.
As said in #1 :
Quote:
the concerning parameter start with a tab char (but should be one or more space).
Quote:
Originally Posted by Don Cragun
In post #1 you didn't mention anything about comment lines nor about empty lines that are not supposed to be sorted, but there are lines at the start of the file you uploaded that I assume are not intended to be sorted. (But, of course, that is a wild assumption because you have not really shown us what output should be produced by processing the file you uploaded.)
.....
......
You also mention in post #1:
but there aren't any <tab> characters in your example in post #1 and there aren't any leading <space> characters in the file you uploaded.
In post #5 "---------- Post updated at 16:34 ---------- Previous update was at 16:17 ----------"
I explain what my script is doing
Quote:
Took initial file.
Remove comments.
remove blank lines.
sort the file.
Add my header.
And what must be corrected
That give the file I just provide.
In that file :
line 28 must be the first position of the three (new position)
line 54 must follow in second position (new position)
line 27 must the last in third position (new position)
But the lines position are unknown.
Only the contents of the offending lines are known. [/QUOTE]
Quote:
Assuming that you want all lines
..............
.......
With the sample data you uploaded, this produces the output:
If this isn't what you're trying to do, please specify much more clearly exactly what it is that you're trying to do and show us the output you are trying to produce.
As said in post #5, I need that line 27,28,54 re-ordered as shown in post #1 in last block of code.
Hi,
I have 4 parameters passed to my shell and i validate if all four are entered using the following snippet:
if
then echo "Not entered"
else echo "entered"
fi
I get the following output as 'Not entered even when i enter the values for all prompts.
Please advise.
Thanks. (5 Replies)
Hi,
I have a script which will be executed using the below command,
bin/nutch crawl urls -dir /data/test/
bin/nutch - Script file
crawl, urls, /data/test/ - Parameters
-dir - Option
The above script should executed from a shell script named test.sh. I have the below code to execute... (2 Replies)
In a Bash script I used getopts command to let a user does something regards to the selected options. The question is: How do you find out what is the name of the file that user inserted in the command line like the following:
The good part is this file is always the last argument in the... (2 Replies)
I've found a few posts regarding passing parameters to a function or subroutine, but for some reason when I try to run a command based on part with these parameters it's not working. If I have the function echo the parameters they show correctly so I believe they are being passed right but the... (2 Replies)
Hi,
So I have a web page that has some drop down boxes with a whole bunch of entries. Unfortunately, they have been added over time and started from a small list and is now extremely messy. I'm looking to write script so I can just copy in the section of the HTML code and have it sorted for... (6 Replies)
Hi, I am trying to do this thing useing my shell bash ( sorry for my english )
I have in a file 63 hostnames, i wanna ask to the DHCP admin, to reserv that reserves 63 IP addresses of this hosts, using their mac address.
I have thinked this script:
for ((i=1;i<63;i++)); do
arp $(head... (10 Replies)
Hello,
I've got next problem:
I want to examine at the beginning of a script in an if loop that:
1. Is there 4 parameters given
2. If first state is true then: is there switches -e and -d?
3. At the end, how can i indentify them as variebles regardlees to its order.
I was thinking like... (2 Replies)
Hi I have 2 files a.c and a.bak where I changed long to int using awk script.
I want to get the list of functions whose parameters got modified
for eg: fun ( long a, long b ) might be changed to fun ( int a, int b ) (1 Reply)
Hi, I'm a pretty big fan of BASH scripting. I've got a bunch I use for random things and lately a couple issues have been plaguing me.
Both are somewhat related, in that they deal with filenames with spaces and "escaped" characters and with launching a program with command line arguements... (5 Replies)