awk, sed and a shell doubts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk, sed and a shell doubts
# 1  
Old 08-27-2008
awk, sed and a shell doubts

Hi,

I have 3 doubts which I posted it here.

Doubt 1:
I have a file containing data:

22 -73 89 10
99 21 15 -77
23 63 -80 91
-22 65 28 97

I am trying to print the fields in the reverse order and replace every field by its absolute (positive) value
(ie.) I am looking for output:

10 89 73 22
77 15 21 99
91 80 63 23
97 28 65 22

when I use the following command

Code:
awk '{ for ( i = NF; i > 0; i-- ) if ($i < 0) $i = -$i; print $i }' <file>

it gives o/p

22 73 89 10
99 21 15 77
23 63 80 91
22 65 28 97

How do I get the output which I need?


Doubt 2:
what is the difference between the following 2 sed commands? And, also please explain the logic behind these commands, it's more obscure to understand.
It does remove all linefeed chars and make multiple lines into a single line.
Code:
sed ':a;$!{N;s/\n//;ba;}'
sed ':a;$!{N;ba;};$s/\n/ /g'


Doubt 3:
what is the difference between the following 2 shell commands?
I know they both sets the positional parameters to the values of $var
Code:
set $var  
set -- $var

# 2  
Old 08-27-2008
The print $i is not inside the for loop so it simply evaluates to print $0 which of course prints the whole line.

The sed scripts mainly differ in how newlines are neutralized. The first will read a line, remove its newline, and loop. The second reads in lines as long as there are any, and then finally replaces all newlines with a space each, as a global replacement.

The double dashes are just a security device; if $var were to contain "-x" then the command would be set -x which does not set the positional parameters of the shell. The double dash says that even if the following argument were to start with a dash, it's not an option. It's good form to always put the double dash when you want to set the positional parameters.

It might be better to post separate threads for separate questions.
# 3  
Old 08-27-2008
1.) awk '{ gsub(/-/,"",$0); for ( i = NF; i > 0; i-- ) printf("%s ",$i); printf("\n");}' <file>
# 4  
Old 08-27-2008
the another way to do the 1st :
Code:
awk '{for (i=NF; i>=1; i--) printf ("%d ",($i>0) ? $i : -1*$i); printf "\n"}'  /path/of/file

# 5  
Old 08-27-2008
Code:
sed ':a;$!{N;s/\n//;ba;}'

maybe i can explain some details of this script:
:a : the label of the branch (u will see ba in the end of script), u can use any alphabet as u wish, so that's a loop of sed
$!: except the last line will do the following actions :
N: read the next line into the pattern space, so, there are two lines in the pattern space
s/\n/ /: replace the \n with the " ", so that will combine the two lines to one
ba: return to the top of the script until the all lines have been read, print it!

i hope that will helpful :-)

Last edited by gnsxhj; 08-27-2008 at 05:57 PM..
# 6  
Old 08-28-2008
Quote:
Originally Posted by gnsxhj
Code:
sed ':a;$!{N;s/\n//;ba;}'

maybe i can explain some details of this script:
:a : the label of the branch (u will see ba in the end of script), u can use any alphabet as u wish, so that's a loop of sed
$!: except the last line will do the following actions :
N: read the next line into the pattern space, so, there are two lines in the pattern space
s/\n/ /: replace the \n with the " ", so that will combine the two lines to one
ba: return to the top of the script until the all lines have been read, print it!

i hope that will helpful :-)
Great explanations. Thanks to era, sudhamacs, gnsxhj

Now, I can explain this script's logic by my own:

Code:
sed ':a;$!{N;ba;}; $s/\n/ /g'

:a -- label of the branch that acts as a loop of sed until ba, any alphabet can be used
$! -- except the last line do the following actions:
N -- read next line into the pattern space (adding up multiple lines separated by '\n')
ba -- return to the top of the script until all the lines are read and then print it
s/\n/ /g -- replace '\n' with "<space>" globally in the pattern space, that combines all the lines in to one
# 7  
Old 08-29-2008
Code:
nawk '{
for (i=NF;i>=1;i--)
printf("%s ",($i>0?$i:(0-$i)))
print ""
}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help shell scripting using awk or sed or other

I need to create a script to change a file depending of 3 conditions using a target as parameter... first condition <chamada> <numeroTerminalOriginador>CALLER</numeroTerminalOriginador> <imeiOriginador></imeiOriginador> <cgiPrimeiraErbOriginador></cgiPrimeiraErbOriginador>... (2 Replies)
Discussion started by: poulis
2 Replies

2. Shell Programming and Scripting

Basic sed doubts

Hi, I am a newbie to sed environment. Have been trying to understand some basics of the language, I am required to replace the occurence of a text string with 0...the command that has been told to me is this "s/^\s<=k/0/" -e Can some one please explain me how to break the command for... (4 Replies)
Discussion started by: seddoubt
4 Replies

3. UNIX for Dummies Questions & Answers

Doubts About awk, and Gawk

well i have some doubts about the use of this commands: my first doubt is to know if there is a way to execute a awk program from a file? (now i do copy paste, i copy the script of a notepad on the terminal and then i press enter, but i want to put this scripts in some folder and execute them)... (3 Replies)
Discussion started by: matius_88
3 Replies

4. Shell Programming and Scripting

Shell script doubts

Hello please kindly solve these doubts i have about the following scripts Script 1 //Shell script that accepts arguments and prints them in reverse order// echo "number of arguments" len=$#; while do eval echo \$$len len=`expr $len - 1` done --------- Output $sh test.sh a... (3 Replies)
Discussion started by: Shynash21
3 Replies

5. Programming

Shell script using sed or awk

Hi, I want to read a file from the command line and remove all the spaces, tabs in it, replacing it with comma(,), and write it to a new file. can you help me out with this scenario. (1 Reply)
Discussion started by: sudhanshu12788
1 Replies

6. Shell Programming and Scripting

Awk miscellaneous doubts

Hi, I am having the following doubts on awk. Please clarify for me. a) What does it mean? awk '$1=$1' and how does it change if I append FS="" to the above code? b) What if I use awk -vFS="\n" (i.e) setting (input) field separator to newline char, then what will be the value of $0,... (6 Replies)
Discussion started by: royalibrahim
6 Replies

7. Shell Programming and Scripting

shell scripting doubts

Hello All, I am working in a reputed software firm,currently working on testing platform (manual and automation).But I am very much intersted in unix and shell scripting,I feel that I am good at beginner level of shell scripting,still I want to practise shell scripting to become excel in it.I... (1 Reply)
Discussion started by: maheshglm
1 Replies

8. Shell Programming and Scripting

awk miscellaneous doubts

In this following command: awk 'BEGIN{ORS=""}1' what does '1' signifies that comes after closing curly brace '}' of awk? I guess, it does not mean 'first occurrence' because I verified that. And, pls tell me how to override or suppress awk's field variables like $1, $2.. by positional... (7 Replies)
Discussion started by: royalibrahim
7 Replies

9. Shell Programming and Scripting

Few Important doubts and questions..(Unix/Shell)

Hi All, I have a few questions which I am unable to solve/answer. Please help me with them:- Command in Unix to determine if a Host is connected to the internet ? If a parenet process is killed what happens to the child process? How do I print the first 10 lines of a file in the... (1 Reply)
Discussion started by: shubhranshu
1 Replies

10. Shell Programming and Scripting

Help on SED AWK in shell scripting

Hi, I have a script abc.sql which contains a word 'timestamp'. I have another script xyz.txt genrated everyweek, which has a new timestamp value every week. How do I replace the word 'timestamp' in script abc.sql with the value mentioned in the script xyz.txt, so that I can run the script... (3 Replies)
Discussion started by: kaushys
3 Replies
Login or Register to Ask a Question