I regularly do this. So I automated it.
Below is a small simple script for copying files which are present in two different directories but without same names into a single directory with having name collision.
#!/usr/bin/sh
for i in `find <source_dir_path> -type f`
do
new=`echo $i|nawk -F"/" '{split($NF,a,".");print "<target_dir_path>"a[1]"_"$(NF-1)"."a[2]}'`
cp $i $new
done
There was a requirement once where i need to compare two files not with thier rows instead i needed to do the comparision with columns.
I wanted only those rows where any of the columns in the lines differ.
for eg:
File1
1 A B C D
2 E F G H
File2
1 A Z C D
2 E F Y H
3 M N O P
Below is the Output I need:
file1 1 col2 B
file2 1 col2 Z
file1 2 col3 G
file2 2 col3 Y
Below is the solution in awk that i have written.
awk 'FNR==NR{a[FNR]=$0;next} {
if(a[FNR])
{split(a[FNR],b);
for(i=1;i<=NF;i++)
{
if($i!=b[i])
{
printf "file1 "b[1]" col"b[i-1]" "b[i]"\n";
printf "file2 "$1" col"b[i-1]" "$i"\n";
}
}
}
}'
Below is the test i made on my solaris server:
> nawk 'FNR==NR{a[FNR]=$0;next}{if(a[FNR]){split(a[FNR],b);for(i=1;i<=NF;i++){if($i!=b[i]){printf "file1 "b[1]" col"i-1" "b[i]"\n";printf "file2 "$1" col"i-1" "$i"\n";}}}}' file1 file2
file1 1 col2 B
file2 1 col2 Z
file1 2 col3 G
file2 2 col3 Y
>
I have simple perl script as below:
#!/usr/bin/perl
use strict;
use warnings;
print "hello ! world\n";
I can execute this script as below:
>temp.pl
hello ! world
>
if i add some comments like this:
#this script is just for test
#the shebang
#!/usr/bin/perl
use strict;
use warnings;
print "hello ! world\n";
and when i try to execute,it gives me output as below:
> temp.pl
use: Command not found.
use: Command not found.
print: Command not found.
>
The point here is shebang line should be always at the top no matter what. but can anybody explain me why?
What wiki says?
In computing, a shebang (also called a sha-bang,hashbang,pound-bang,hash-exclam,or hash-pling) is the character sequence consisting of the characters number sign and exclamation mark (that is, "#!") when it occurs as the initial two characters on the initial line of a script.
Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.[8] For example, if a script is named with the path "path/to/script", and it starts with the following line:
Why should it always be the first line?
The shebang must be the first line because it is interpreted by the kernel, which looks at the two bytes at the start of an executable file. If these are #! the rest of the line is interpreted as the executable to run and with the script file available to that program on stdin. (Details vary slightly, but that is the picture).
Since the kernel will only look at the first two characters and has no notion of further lines, you must place the hash bang in line 1.
Now what happens if the kernel can't execute a file beginning with #whatever? The shell, attempting to fork an executable and being informed by the kernel that it can't execute the program, as a last resort attempts to interpret the file contents as a shell script. Since the shell is not perl, you get a bunch of errors, exactly the same as if you attempted to run
I have a file like this.
John
30
Mike
0.0786268
Tyson
0.114889
Gabriel
0.176072
Fiona
0.101895
I need to shift every second row to a new column so it should look like this
John 30
Mike 0.0786268
Tyson 0.114889
Gabriel 0.176072
Fiona 0.101895
There can be many solutions for this:
sed:
sed 'N;s/\n/ /g' yourfile
Awk:
awk '{if(NR%2!=0){p=""}else{p="\n"};printf $0" "p}' your_file
Paste:
paste - - file new_file
My pick is:
xargs -n2
Below is a most simple command for Sorting Unix processes by highest memory usage:
ps -eo pmem,pid,pcpu,rss,vsz,time,args | sort -k 1 -r