Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have to get the version number of uboot stored in a file. The location of the file is tmp/version.
The output of the file is something like
U-Boot 2011.01 8.5.R02.149 |armv7_ac3| (Jan 27 2021 - 21:28:06)
The number in bold letters is the version number. I have to store this version number to a variable
I tried
uboot = `cat tmp/version`
version = "${uboot:24:27}"
but I don't think it is the correct way of doing it. Please suggest if there is any other way of getting the required output
The Internal Field Separator IFS environment variable can be set to delimit the different version fields to read from the file.
If you are only interested by the build version, you can skip un-needed fields using _ as place-holder variable name:
#!/usr/bin/env bash
IFS='. ' read -r _ _ _ _ _ _ version _ <tmp/version
Here is how you can collect all of it at once:
#!/usr/bin/env bash
IFS='. ' read -r name year month major minor release build _ <a.txt
# For debug purpose
declare -p name year month major minor release build
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have some directory paths.
dir1/abc/dir2/dir3
and
dir1/xyz/dir2/dir3
I have a Perl script that manipulates these directories, but that script is running into errors with path names.
How can I set "abc" = "xyz" in a perl variable, so that I can use that variable in the pathnames above?
For example, it should be
dir1/$PathVariable/dir2/dir3
so that the script doesn't care whether part of the path is "abc" or "xyz".
So far, I tried using the eq keyword
my $Path1 = "abc";
my $Path2 = "xyz";
my %PathVariable = $Path1 eq $Path2;
Turns out eq doesn't do what I thought. Any tips?
I think this is your question. You have a path template such as dir1/$PathVariable/dir2/dir3 and you want to fill in the $PathVariable with different values.
The quick way to do that is simple double-quoted interpolation:
my $PathVariable = 'abc';
my $directory = "dir1/$PathVariable/dir2/dir3";
print "Dir is <$directory>\n"; # dir1/abc/dir2/dir3
But, you want to do this for a few directories. Iterate through a list of the values you want. Each time through the foreach, $PathVariable gets the next value from #paths:
my #paths = qw(abc xyz);
foreach my $PathVariable ( #paths ) {
my $directory = "dir1/$PathVariable/dir2/dir3";
print "Dir is <$directory>\n";
... do whatever you need to do ...
}
Now, having figured that part out, there are a few things to think about when creating paths. The File::Spec module that comes with Perl knows how to put together paths appropriate for the system that you are working on. It's a good habit to have so you avoid weird cases:
use File::Spec::Functions;
my $dir = catfile( 'dir1', $PathVariable, 'dir2', 'dir3' );
There are other CPAN modules that do the job too, but that might be a bit much until you solve this issue.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have system logs where alarms are written. in my case i have lots of repeated alarms which i want to ignore and focus only on new alarms that might be exist.
sample alarm :
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- <0N CpocsSs7CircuitCat#040200000000009a|6501646464644309|6501646464c90000-6503010117c80000-1.0.3|a40200003e3d8fd5|0000000e|0000000000000000
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- |RC USSDString=*1234*#|MSISDN=93707678224|
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- |NF NOT: src=ERROR_APPLICATION sev=SEVERITY_MAJOR id=010117c800000001
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- de.siemens.advantage.in.featureframework.FeatureException: GenAcc> [0]theGenericAccess: No value available for SubsDMDB.Subscriber.LanguageID and type INTEGER
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- at de.siemens.advantage.in.features.genericAccess.impl.DynamicAsciiBuffer$Handle.throwNotAvailableException(DynamicAsciiBuffer.java:1105)
--
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- <0N CpocsSs7CircuitCat#020200000000008a|6501646464644309|6501646464c90000-6501646464640000-1.1.1|a20200003cc
31dd2|0000000e|0000000000000000
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- |RC CdPA=173|CgPA=93705040139|
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- |NF NOT: src=ERROR_APPLICATION sev=SEVERITY_MAJOR id=6503010103c80016
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- Exception in flexible core (e.g. during logic execution):de.siemens.advantage.in.featureframework.FeatureExc
eption: Call.checkIfCcOperationIsAllowed(): operation Call.playAnnouncement() only allowed within an open call control dialog
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- at de.siemens.advantage.in.features.flexDTMF.actions.dtmfActions.impl.DTMFActionsController.playAnnounc
ementList(DTMFActionsController.java:360)
--
the above lines are related to one alarms. here i want to omit such alarm in my log file.
I have tried using grep -v 'RC USSDString' IN-201609201800.txt | more but this command removes only the line where the searched pattern grep -v 'RC USSDString' IN-201609201800.txt | more does exist, where i want to remove the entire lines of alarm where pattern is found.
Edit:
- I have added one more alarm separated by double dash
Assuming your alarms are multi line and two alarms are separated with each other by --.
awk -v RS="--" '{$1=$1} !/RC USSDString/' alarmfile
If you want to add , multiple string to be excluded from output then:
awk -v RS="--" '{$1=$1} !/string-1/ || !/string-2/' alarmfile
What you have to do :
grep -Ev 'pattern1|pattern2|pattern3' file
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I need to do is, to check for duplicate domain names and find if there is some.
So far I tried many commands with grep, awk ,sort, uniq but couldn't work this out, I am feeling its very simple, but can't reach it.
P.s. If i use uniq -c I get a huge list of string in this file, and I see how many duplicates it has and which by number string it is.
adding 20 rows from the file I am using
1,google.com
2,facebook.com
3,youtube.com
4,yahoo.com
5,baidu.com
6,amazon.com
7,wikipedia.org
8,twitter.com
9,taobao.com
10,qq.com
11,google.co.in
12,live.com
13,sina.com.cn
14,weibo.com
15,linkedin.com
16,yahoo.co.jp
17,tmall.com
18,blogspot.com
19,ebay.com
20,hao123.com
The output I would like to see
> 2 google
> 2 yahoo
Thanks for help !
You could use something like this to get the output you want:
$ awk -F'[.,]' '{++a[$2]}END{for(i in a)if(a[i]>1)print a[i],i}' file
2 google
2 yahoo
With the input field separator to either . or ,, the first {block} is run for every row in the file. It builds up an array a using the second field: "google", "facebook", etc. $2 is the value of the second field, so ++a[$2] increments the value of the array a["google"], a["facebook"], etc. This means that the value in the array increases by one every time the same name is seen.
Once the whole file is processed, the for (i in a) loop runs through all of the keys in the array ("google", "facebook", etc.) and prints those whose value is greater than 1.
Given this file:
$ cat /tmp/test.txt
1,google.com
2,facebook.com
3,youtube.com
4,yahoo.com
5,baidu.com
6,amazon.com
7,wikipedia.org
8,twitter.com
9,taobao.com
10,qq.com
11,google.co.in
12,live.com
13,sina.com.cn
14,weibo.com
15,linkedin.com
16,yahoo.co.jp
17,tmall.com
18,blogspot.com
19,ebay.com
20,hao123.com
In a Perl 1 liner:
$ perl -lane '$count{$1}++ if /^\d+,(\w+)/; END {while (($k, $v) = each %count) { print "$v $k" if $v>1}}' /tmp/test.txt
2 yahoo
2 google
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm a bit stuck with this. I'm delaring a variable at the top of my script, then I am creating a file as part of my script:
app="testing"
cat <<EOF >/etc/init.d/test
#!/bin/bash
args="--emperor $APPCONF/test/$APP.ini"
EOF
It doesn't seem to work though, it seems on the $app variable. Must I do something to this variable to get it to display it's value, "testing" inside the file I create?
Use consistent case. variable names are case sensitive.
Let's say you were doing this the Right Way. You'd want to store your data in an array:
args=( --emperor "${appconf}/test/${app}.ini" )
and then convert it to a string for embedding:
printf -v args_str '%q ' "${args[#]}"`
...and use that string inside your heredoc:
#!/bin/bash
args=( $args_str )
EOF
...beyond which, anything inside the script being created would want to expand it as an array:
run_something "${args[#]}"
See BashFAQ #50 for rationale and details.
Besides using consistent case ($app is different from $APP), you may want to enclose your variable names within brackets - you may get issues if you use spaces in between your variables values otherwise, and it's considered a good practice. For example:
args="--emperor ${APPCONF}/test/${APP}.ini"
That way, $APPCONF does not get confused with ${APP}CONF also. I hope this helps!
I'm not sure to understand your question. I suppose that you would like to end with a file
/etc/init.d/test
containing the text:
#!/bin/bash
args="--emperor $APPCONF/test/testing.ini"
if so your script should be:
app="testing"
cat <<EOF >/etc/init.d/test
#!/bin/bash
args="--emperor \$APPCONF/test/$app.ini"
EOF
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have two files , one as a template and other as a input file for next command. I have to update my input file based on the values in template
First file looks like as shown below
TKTSPEC.2.ASSETATTRID=REVISION&
TKTSPEC.2.REFOBJECTID=31&
TKTSPEC.2.TICKETSPECID=410&
TKTSPEC.2.SECTION=&
TKTSPEC.3.ASSETATTRID=NUM&
TKTSPEC.3.REFOBJECTID=31&
TKTSPEC.3.TICKETSPECID=411&
TKTSPEC.3.SECTION=&
TKTSPEC.4.ASSETATTRID=MPNUM&
TKTSPEC.4.REFOBJECTID=31&
TKTSPEC.4.TICKETSPECID=412&
TKTSPEC.4.SECTION=&
My Template file looks like
TKTSPEC.2.ASSETATTRID=REVISION&
TKTSPEC.2.TABLEVALUE=5&
TKTSPEC.3.ASSETATTRID=NUM&
TKTSPEC.3.TABLEVALUE=RDPVS&
TKTSPEC.4.ASSETATTRID=MPNUM&
TKTSPEC.4.TABLEVALUE=NEWPROJECT&
My Desired output is as follows
TKTSPEC.2.ASSETATTRID=REVISION&
TKTSPEC.2.TABLEVALUE=5&
TKTSPEC.2.REFOBJECTID=31&
TKTSPEC.2.TICKETSPECID=410&
TKTSPEC.2.SECTION=&
TKTSPEC.3.ASSETATTRID=NUM&
TKTSPEC.3.TABLEVALUE=RDPVS&
TKTSPEC.3.REFOBJECTID=31&
TKTSPEC.3.TICKETSPECID=411&
TKTSPEC.3.SECTION=&
TKTSPEC.4.ASSETATTRID=MPNUM&
TKTSPEC.4.TABLEVALUE=NEWPROJECT&
TKTSPEC.4.REFOBJECTID=31&
TKTSPEC.4.TICKETSPECID=412&
TKTSPEC.4.SECTION=&
I have to check the ASSETATTRID from my first file and then insert a new line with corresponding value from the second file.Second file has value for every assetattrid.
Can this be acheived using awk or other linux based commands ?
One way:
awk -F. 'NR==FNR{getline x;a[$2$3]=x;next}$2$3 in a{print;print a[$2$3];next}1' templatefile inpfile
this oneliner may work for you:
awk 'NR==FNR{k=$0;getline;a[k]=$0;next}$0 in a{$0=$0"\n"a[$0]}1' templ input