find and replace line with variable use sed - bash

sn=$(./libs/ideviceinfo | grep ^SerialNumber | awk {'printf $NF'})
type=$(./libs/ideviceinfo | grep ProductType | awk {'printf $NF'})
udid=$(./libs/ideviceinfo | grep UniqueDeviceID | awk {'printf $NF'})
I want to replace variable value into this txt file
{
"InternationalMobileEquipmentIdentity" = "355331088790894";
"SerialNumber" = "C6KSJM0AHG6W";
"InternationalMobileSubscriberIdentity" = "";
"ProductType" = "iPhone9,1";
"UniqueDeviceID" = "69bae2fcc0da3e6e3373f583ef856e02c88026eb";
"ActivationRandomness" = "25E7742B-76A7-4C31-9F49-52D17A817B2F";
"ActivityURL" = "https://albert.apple.com/deviceservices/activity";
"IntegratedCircuitCardIdentity" = "";
"CertificateURL" = "https://albert.apple.com/deviceservices/certifyMe";
"PhoneNumberNotificationURL" = "https://albert.apple.com/deviceservices/phoneHome";
"ActivationTicket" = "";
}
i try using sed:
sed 's/"SerialNumber.*/"SerialNumber" = "$sn";/g' ./file/bp.txt > ./file/bp1.txt
The output is not as expected: "SerialNumber" = "$sn";
Hope you guys can help me
p/s: can you help me if 1 command can replace 3 variable values ​​at the same time, that would be great

The problem here is one of shell quoting. Using single quotes means that everything inside will not go through substitution.
The following should fix your problem:
sed 's/"SerialNumber.*/"SerialNumber" = "'"$sn"'";/g' ./file/bp.txt > ./file/bp1.txt

Related

Use sed to remove string, add whitespace and insert ""

I have a file with the following entry:
export TF_VAR_environment_name=dev
export TF_VAR_project_name=hello-world
I would like to do 3 things with these enteries:
Remove the export TF_VAR_ string
Add whitespace to both sides of =
Wrap the string right of = in " "
So my file would end up looking like:
environment_name = "dev"
project_name = "hello-world"
I'm able to remove the string with s/"export TF_VAR_"//, but haven't been able to wrap the = in whitespace, or wrap the final string in quotes. Any help would be greatly appriciated.
Is this possible to do in sed?
input.txt is your textfile.
output.txt is the wanted result.
sed 's/export TF_VAR_// ; s/=\ (.*\ )$/ = "\1"/ ' < input.txt > output.txt
there is no blank between \ and (
and no blank between \ and )
input.txt is your textfile. output.txt is the wanted result.
sed 's/export TF_VAR_// ; s/=\(.*\)$/ = "\1"/ ' < input.txt > output.txt
its the same as above. i just tried to post it here in a correct way.

Substring in C Shell not working as intended

I'm trying to extract the file name without the extension from a directory full of files. Here is the code that I'm using:
foreach file (*)
set extPos=`echo $file | awk '{print index($0,".")}'`
set fname = `echo $file | awk '{print substr($0,0,$extPos)}'`
echo $file
echo $extPos
echo $fname
end
And these are the results I'm receiving:
hodorrr.png
8
testfile1.txt
10
testfile2.txt
10
testfile3.txt
10
wtf.tiff
4
So as you can see the substrings are blank, does anyone know why this is?
The usage of substr can be confusing and inconsistent with other versions of substr.
To fix your immediate problem, try
set fname = `echo $file | awk '{print substr($0,1,'"$extPos"')}'`
To eliminate the relatively expensive calculation of $extPos with multiple processes, you can get fname from file all in 1 awk process with
file=testfile1.txt
#not needed set extPos=`echo $file | awk '{print index($0,".")}'`
set fname = `echo $file | awk '{print substr($0,1,index($0,".")-1)}'`
echo "fname=" $fname
testfile1
So note that substr doesn't use 0 for it's address for the first position in a string (but uses 1 (thank you awk gods!;-) AND that you can nest function calls (like index()) inside the parameter list of another function.
Of course the usual warnings like friends don't let friend code in csh still apply, but sometime organization inertia is too much to overcome!
IHTH
If I understand what you're trying to do, you don't need awk. csh has built-in features to extract the root or extension from a file name.
% foreach file ( this.txt that.dat another.blah )
foreach? echo "file = '$file', root = '$file:r', extension = '$file:e'"
foreach? end
file = 'this.txt', root = 'this', extension = 'txt'
file = 'that.dat', root = 'that', extension = 'dat'
file = 'another.blah', root = 'another', extension = 'blah'
%
The modifiers are documented in the tcsh man page under History substitution, but they also apply to variable substitution.
(If you decide to switch to bash, it has similar features, but they're not quite as convenient.)

grep to ignore the first character

If I have a bunch of lines that have :
//mainHtml = "https://
mainHtml = "https:
//https:www.google.com
public String ydmlHtml = "https://remando
aasdfa dsfadsf a asdfasd fasd fsdafsdaf
Now I want to grep only those lines which have "https:" in them, but they should NOT start with "//"
So far I have :
cat $javaFile | grep -e '\^\/ *https:*\'
where $javaFile is the file I want to look for the words.
My output is a blank.
Please help :)
You can use character class to negate the start of lines. We use -E option to use ERE or Extended Regular Expression.
grep -E '^[^/]{2}.*https' file
With your sample data:
$ cat file
//mainHtml = "https://
mainHtml = "https:
//https:www.google.com
public String ydmlHtml = "https://remando
aasdfa dsfadsf a asdfasd fasd fsdafsdaf
$ grep -E '^[^/]{2}.*https' file
mainHtml = "https:
public String ydmlHtml = "https://remando
You may also choose to write it without the -E option by saying:
grep '^[^/][^/].*https' file
In two steps:
grep -v '^//' | grep 'https:'
grep -v '^//' removes the lines starting with //
grep 'https:' gets the lines containing http:

how can I convert this string into a list through the command line

I have files that are named like C1_1_B_(1)IMG1511.jpg and I want to split them up into a list where i get back as
C1
1
B
(1)
IMG1511.jpg
trying to figure out if i need to do this with sed or awk or regex or even what that would look like i could do it in applescript but I would rather call shell command as it is much faster
EDIT
Ok so now its changed a bit and I can figure out how to fix it
example are
"P24-M_(1)Lighter_Ray_Logo_Full_Color.jpg"
"P24_(1)24x36loren.jpg"
so _(*) indicates where I want to stop listing so i end up with
P24
M
(1)
Lighter_Ray_Logo_Full_Color.jpg
and
P24
(1)
24x36loren.jpg
Translate _ to new lines:
echo "C1_1_B_(1)IMG1511.jpg" | tr '_' '\n'
Output:
C1
1
B
(1)IMG1511.jpg
Although, it looks like you want to split on ) as well. No can do with tr, but...
echo "C1_1_B_(1)IMG1511.jpg" | tr '_' '\n' | sed -e 's/)/)\
/'
There's a linefeed inside the replacement string, which is needed for Mac. On other *nix OS's, a simple escape works:
echo "C1_1_B_(1)IMG1511.jpg" | tr '_' '\n' | sed -e 's/)/)\n/'
Output:
C1
1
B
(1)
IMG1511.jpg
Would this do?
<<<"C1_1_B_(1)IMG1511.jpg" sed -r 'y/_/\n/;s/\([^)]*\)/&\n/g;'
I know it's not sed/awk, but here's something that would work in perl:
#!/usr/bin/perl
while(<STDIN>) {
my($line) = $_;
chomp($line);
my #values = split(/_|(\(\d+\))/, $line);
foreach my $val (#values) {
if ( $val !~ m/^$/)
{
print "$val\n";
}
}
}
exit 0;
If the filename is stored in $P, the following works with zsh:
myarr=${(s/_/)$(echo $P | sed 's/)/)_/g')}
This creates an actual array.
This handles filenames which contain _ ( ) in other places.
<<< '
C1_1_B_(1)IMG151).jpg
C1_1_B_(1)IMG_(4444).jpg
C(22)2_1_22_333_B_(144)I_M_G_(_1511).jpg
' sed -nr '# isolate, process and print first section
s/^([^(]+)_/\1\n/;h
s/(.*)\n.*/\1/
s/([^_]+)_/\1\n/gp;x
# process the second section
s/.*\n(.*)/\1/
s/([^)]+\))/\1\n/p
';exit
str="C1_1_B_(1)IMG1511.jpg"
ary=( $(IFS=_; echo $str) )
for ((idx=0; idx < ${#ary[#]}; idx++)); do echo ${ary[$idx]}; done
outputs
C1
1
B
(1)IMG1511.jpg

Find text value in file, print it out?

I've got a shell script. I want to open a file, and copy out a bit of text from the file. Example:
// foo.java
public static int ID_RED = 100;
public static int ID_GREEN = 200;
public static int ID_BLUE = 300;
// pseudo-code:
int pos = find("public static int ID_RED = ");
echo(file.substring(pos, end of line);
pos = find("public static int ID_GREEN = ");
echo(file.substring(pos, end of line);
pos = find("public static int ID_BLUE = ");
echo(file.substring(pos, end of line);
// desired output:
100
200
300
So I want to open foo.java, and print out the values found at the end of those lines. I think it'd be easier to do this in perl or python, wanted to see if there was a simple way to do this in a shell script, though,
sed -n '/^public static int ID_/{s/;.*$//;s/.* //p;}' foo.java
This doesn't exactly match the criteria implied by your pseudo-code, but it does the job for this particular input. Consider replacing ^public by ^ *public if there might be leading whitespace. Replace ID_ by ID_\(RED\|GREEN\|BLUE\)\> if you don't want to match ID_YELLOW, for example.
This should help you get started:
#!/bin/bash
grep "public static int ID_RED = " foo.java | cut -d " " -f 6 | tr -d \;
grep "public static int ID_GREEN = " foo.java | cut -d " " -f 6 | tr -d \;
grep "public static int ID_BLUE = " foo.java | cut -d " " -f 6 | tr -d \;
awk -F '[ ;]' '/public static int/ {print $(NF-1)}' foo.java
or
sed -n '/public static int/ {s/^.*= *\([0-9]\+\);/\1/; p}' foo.java

Resources