UNIX:
I have to load file contents to one table when it will find the tag:
ACC2020000
Contents in file:
ACC2020000 ALEJA B JURI
Tried with below code :
if(substr($_,0,10) eq 'ACC2020000')
{
$ADDRESS1= (split(" ",$_))[1];
}
Output : ALEJA
Expected Output : ALEJA B JURI
Can anyone suggest how to get the correct output?
You can do this with grep and cut easily, assuming there is a space after the ACC2020000 pattern:
grep '^ACC2020000' file | cut -f2- -d' '
Related
How do I cut characters from column 5 to 7 of the lines 3 onwards?
I am trying to use sed/cut.
For example, If I have
this is amazing1 this is amazing11
this is amazing2 this is amazing21
this is amazing3 this is amazing31
this is amazing4 this is amazing41
this is amazing5 this is amazing51
this is amazing6 this is amazing61
this is amazing7 this is amazing71
Output should look like:
this is amazing1 this is amazing11
this is amazing2 this is amazing21
this amazing3 this is amazing31
this amazing4 this is amazing41
this amazing5 this is amazing51
this amazing6 this is amazing61
this amazing7 this is amazing71
The characters is are removed from lines 3 and onwards.
sed -E '3,$s/(....).../\1/' file
I'd just use awk for clarity, portability, etc.:
$ awk 'NR>2{$0=substr($0,1,4) substr($0,8)} 1' file
this is amazing1 this is amazing11
this is amazing2 this is amazing21
this amazing3 this is amazing31
this amazing4 this is amazing41
this amazing5 this is amazing51
this amazing6 this is amazing61
this amazing7 this is amazing71
or using variables populated with the values from your question:
$ awk -v n=3 -v beg=5 -v end=7 'NR>=n{$0=substr($0,1,beg-1) substr($0,end+1)} 1' file
this is amazing1 this is amazing11
this is amazing2 this is amazing21
this amazing3 this is amazing31
this amazing4 this is amazing41
this amazing5 this is amazing51
this amazing6 this is amazing61
this amazing7 this is amazing71
In two steps:
head -n2 infile; tail -n+3 infile | cut --complement -c5-7
The first command prints the first two lines unmodified; the second command pipes the lines starting with the third one to cut, where character 5 to 7 are removed (requires GNU cut).
If you need to do something with the output, like store it in a file, you have to group these commands before redirecting:
{
head -n2 infile
tail -n+3 infile | cut --complement -c5-7
} > outfile
If you want to use sed:
sed '1,2!s/^\(\w*\)\s*\w*\(.*\)$/\1\2/' file
DETAILS
1,2!s - Don't do substitutions on line 1 and 2.
/^\(\w*\)\s*\w*\(.*\)$/ - The matching pattern.
/\1\2/ - Restore the groups of 1 and 2.
file - Your input file.
Im working with database which looks very common:
age:position:name:
I still don't know why this database is made in this order, but for better orientation and manipulation, I would like to reverse the order like this:
name:age:position.
I'm trying to do it with unix like this:
datab=`cut -d : -f1,2,3 inf.major`
age=`echo "$datab" | cut -d : -f1 > age`
pos=`echo "$datab" | cut -d : -f2 > pos`
name=`echo "$datab" | cut -d : -f3 > name`
paste -d : "age" "pos" "name" > inf.major
This is quite laboriously. It would be ok if the data had only few "sections" divided by : but it has more than 10. Is there any way how to achieve the same result but dynamicly/faster?
You can use awk/gawk:
gawk -F":" '{print $3":"$2":"$1;}' inf.major
This will separate each line of your file at : and print the first three elements in reversed order.
I am trying to use awk to get the name of a file given the absolute path to the file.
For example, when given the input path /home/parent/child/filename I would like to get filename
I have tried:
awk -F "/" '{print $5}' input
which works perfectly.
However, I am hard coding $5 which would be incorrect if my input has the following structure:
/home/parent/child1/child2/filename
So a generic solution requires always taking the last field (which will be the filename).
Is there a simple way to do this with the awk substr function?
Use the fact that awk splits the lines in fields based on a field separator, that you can define. Hence, defining the field separator to / you can say:
awk -F "/" '{print $NF}' input
as NF refers to the number of fields of the current record, printing $NF means printing the last one.
So given a file like this:
/home/parent/child1/child2/child3/filename
/home/parent/child1/child2/filename
/home/parent/child1/filename
This would be the output:
$ awk -F"/" '{print $NF}' file
filename
filename
filename
In this case it is better to use basename instead of awk:
$ basename /home/parent/child1/child2/filename
filename
If you're open to a Perl solution, here one similar to fedorqui's awk solution:
perl -F/ -lane 'print $F[-1]' input
-F/ specifies / as the field separator
$F[-1] is the last element in the #F autosplit array
Another option is to use bash parameter substitution.
$ foo="/home/parent/child/filename"
$ echo ${foo##*/}
filename
$ foo="/home/parent/child/child2/filename"
$ echo ${foo##*/}
filename
Like 5 years late, I know, thanks for all the proposals, I used to do this the following way:
$ echo /home/parent/child1/child2/filename | rev | cut -d '/' -f1 | rev
filename
Glad to notice there are better manners
It should be a comment to the basename answer but I haven't enough point.
If you do not use double quotes, basename will not work with path where there is space character:
$ basename /home/foo/bar foo/bar.png
bar
ok with quotes " "
$ basename "/home/foo/bar foo/bar.png"
bar.png
file example
$ cat a
/home/parent/child 1/child 2/child 3/filename1
/home/parent/child 1/child2/filename2
/home/parent/child1/filename3
$ while read b ; do basename "$b" ; done < a
filename1
filename2
filename3
I know I'm like 3 years late on this but....
you should consider parameter expansion, it's built-in and faster.
if your input is in a var, let's say, $var1, just do ${var1##*/}. Look below
$ var1='/home/parent/child1/filename'
$ echo ${var1##*/}
filename
$ var1='/home/parent/child1/child2/filename'
$ echo ${var1##*/}
filename
$ var1='/home/parent/child1/child2/child3/filename'
$ echo ${var1##*/}
filename
you can skip all of that complex regex :
echo '/home/parent/child1/child2/filename' |
mawk '$!_=$-_=$NF' FS='[/]'
filename
2nd to last :
mawk '$!--NF=$NF' FS='/'
child2
3rd last field :
echo '/home/parent/child1/child2/filename' |
mawk '$!--NF=$--NF' FS='[/]'
child1
4th-last :
mawk '$!--NF=$(--NF-!-FS)' FS='/'
echo '/home/parent/child000/child00/child0/child1/child2/filename' |
child0
echo '/home/parent/child1/child2/filename'
parent
major caveat :
- `gawk/nawk` has a slight discrepancy with `mawk` regarding
- how it tracks multiple,
- and potentially conflicting, decrements to `NF`,
- so other than the 1st solution regarding last field,
- the rest for now, are only applicable to `mawk-1/2`
just realized it's much much cleaner this way in mawk/gawk/nawk :
echo '/home/parent/child1/child2/filename' | …
'
awk ++NF FS='.+/' OFS= # updated such that
# root "/" still gets printed
'
filename
You can also use:
sed -n 's/.*\/\([^\/]\{1,\}\)$/\1/p'
or
sed -n 's/.*\/\([^\/]*\)$/\1/p'
I grep a pattern from a directory and the 4 lines before that pattern, I need to further grep the top line from each result , but not getting how to do .
Please suggest regarding this.
The problem explained with example :
in a directory 'direktory'
there are multiple files with different name like 20130611 and 2013400 etc..
the data wrote in the files, which I am interested in is like this :
[
My name is
.....
......
......
Name has been written above
]
now in every instance "Name has been written above" is written in the unit of lines but the value keep on changing in place of "My name is" so I want to grep this particular line from every occurrence .
Please suggest some method to get the result.
Thanks in advance.
a#x:/tmp$ cat namefile
[
My name is
.....
......
......
Name has been written above
]
a#x:/tmp$ cat namefile | grep -B 4 "Name has been written above" | head -1
My name is
Where "4" can be replaced by N i.e. number of lines the target data lies above the grepped line
Try something like
for file in $(ls <wherever>)
do
# Tell the user which file we're looking at
echo ""
echo $file
echo ""
# Output the first line of the file
head -1 $file
# Output the line continaing <pattern> and the four
# preceding lines
<your grep command here>
done
I have an XML file as below:
<xml>Workinstance name="suvi" permission="read" id="6543"</xml>
<xml>Projectinstance name="ram" permission="write" id="3534"</xml>
I want to display the workinstance id field from that XML file.
grep '<xml>Workinstance' file.xml | grep -o 'id="[^"]*' | cut -c5-
$ awk '/Workinstance/{ gsub(/.*id=\042|\042.*/,""); print } ' file
6543