SED: Replacing String After Single Quote - regex

I have a file named file.txt that contains the following:
CREATE LARGE TABLESPACE LONGSPCE1 IN DATABASE PARTITION GROUP IBMDEFAULTGROUP PAGESIZE 4096 MANAGED BY DATABASE
USING (FILE '/db2data1/TGT_INST/TGT_DB/LONGSPCE1.c1' 1588368,
FILE '/db2data2/TGT_INST/TGT_DB/LONGSPCE1.c2' 1588368,
FILE '/db2data3/TGT_INST/TGT_DB/LONGSPCE1.c3' 1588368,
FILE '/db2data4/TGT_INST/TGT_DB/LONGSPCE1.c4' 1588368)
I am trying to change the numerics after the c[0-9]' to a value of 100.
I have tried the following with no luck.
cat file.txt |sed 's/(c1'' )\([0-9]*\)/$1 100/g'

You can use:
sed "s/\(c[0-9]\+'\) [0-9]\+/\1 100/" file.txt
USING (FILE '/db2data1/TGT_INST/TGT_DB/LONGSPCE1.c1' 100,
FILE '/db2data2/TGT_INST/TGT_DB/LONGSPCE1.c2' 100,
FILE '/db2data3/TGT_INST/TGT_DB/LONGSPCE1.c3' 100,
FILE '/db2data4/TGT_INST/TGT_DB/LONGSPCE1.c4' 100)

Related

How to determine if a string is located in AWS S3 CSV file

I have a CSV file in AWS S3.
The file is very large 2.5 Gigabytes
The file has a single column of strings, over 120 million:
apc.com
xyz.com
ggg.com
dddd.com
...
How can I query the file to determine if the string xyz.com is located in the file?
I only need to know if the string is there or not, I don't need to return the file.
Also it will be great if I can pass multiple strings for search and return only the ones that were found in the file.
For example:
Query => ['xyz.com','fds.com','ggg.com']
Will return => ['xyz.com','ggg.com']
The "S3 Select" SelectObjectContent API enables applications to retrieve only a subset of data from an object by using simple SQL expressions. Here's a Python example:
res = client.select_object_content(
Bucket="my-bucket",
Key="my.csv",
ExpressionType="SQL",
InputSerialization={"CSV": { "FileHeaderInfo": "NONE" }}, # or IGNORE, USE
OutputSerialization={"JSON": {}},
Expression="SELECT * FROM S3Object s WHERE _1 IN ['xyz.com', 'ggg.com']") # _1 refers to the first column
See this AWS blog post for an example with output parsing.
If you use the aws s3 cp command you can send the output to stdout:
aws s3 cp s3://yourbucket/foo.csv - | grep 'apc.com'
- The dash will send the output to stdout.
this are two examples of grep checking on multiple patterns:
aws s3 cp s3://yourbucket/foo.csv - | grep -e 'apc.com' -e 'dddd.com'
aws s3 cp s3://yourbucket/foo.csv - | grep 'apc.com\|dddd.com'
To learn more about grep, please look at the manual: GNU Grep 3.7

Rename multiple file with special characters in Fedora

I have several hundred files in the below format:
'[animalpics.com][977]bluejays.png'
'[animalpics.com][9]lions.jpg'
'[animalpics.com][99]colts.jpg'
I would like to rename all the files to just the description and file type:
bluejays.png
lions.jpg
colts.jpg
I have tried rename but it doesn't seem to like the command I'm using:
rename 's/[animalpics.com][*]//g' *.*
How can I edit my command to rename these files? I also have install mmv but it's not as intuitive as I thought.
rename 's/^.*\]//' *
Demo:
$ls -1
'[animalpics.com][977]bluejays.png'
'[animalpics.com][99]colts.jpg'
'[animalpics.com][9]lions.jpg'
file.txt
$rename 's/^.*\]//' *
$ls -1
bluejays.png
colts.jpg
file.txt
lions.jpg
$

cat lines into file after regex

I want to add the info below into the file usr/local/nagios/etc/hosts.cfg but want to do it just below ##company in the hosts.cfg file. My setup script will contain the info that needs to be added
I have spend hours trying to get sed to just add a line into a file after a marker but to no avail
define host{
use linux-box
host_name $host_name
alias $alias
address $ip
parents $parent
notification_period 24x7
notification_interval 5
}
Previously I used
cat <> /path /filename
EOT
but now I need to do it in specif places in the file
Given the following file:
# some content
###company
If I run the following command:
sed -i 's/###company/&\ndefine host {\nuse host\nhost_name HOSTNAME/' file
Now, the contents of file are:
# some content
###company
define host {
use host
host_name HOSTNAME
Is this what you're looking for?

sed command issues

Background
Original json (test.json): {"rpc-password": "password"}
Expected changed json: {"rpc-password": "somepassword"}
replace_json_str is a function used to replace password with somepassword using sed.
replace_json_str() {
x=$1
sed -i -e 's/\({ "'"$2"'":\)"[^"]*" }/\1"'"$3"'" }/g' $x
}
Unit test: replace_json_str test.json rpc-password somepassword
Issue
After running the above test, I get a file named test.json-e and the contents of the file is the same as before the test was ran, why?
there is a handy command line json tool called jq
cat input.json
{"rpc-password": "password"}
cat update_json.sh
givenkey=$1
givenvalue=$2
inputfile=input.json
outfile=output.json
cat $inputfile | jq . # show input json
jq --arg key1 "$givenkey" --arg val1 "$givenvalue" '.[$key1] = $val1' "$inputfile" > "$outfile"
cat "$outfile" | jq . # render output json
keep in mind jq can handle multiple such key value updates ... execute it
update_json.sh rpc-password somepassword
{
"rpc-password": "password"
}
{
"rpc-password": "somepassword"
}
Depends on which sed you're using.
The command you ran will work as expected with GNU sed.
But BSD sed does not allow for an empty argument to -i, and if you ran the same command, it will use the next argument, -e, as the backup file.
Also, the positioning of the spaces in your pattern don't match your example JSON.

multiple name match the pattern

Input file:File name='sample1.txt'
lion is a good friend (Host=lion) (Port=animal) and tiger is
(Host=Tiger)(Port=an)
burger is a food (Host=Burger)(Port=Food)
I have data as shown in the above txt file.I want to collect the host and port in each line from a txt file and place them in new txt file
Required Outfile:
lion:animal
Tiger:an
Burger:Food
Code used till nw:
cat sample1.txt | perl -ne 'print "$1=$2\n" if(/Host=([\w.]*.'-'*[\w.]*.).*Port=(\d+)/)' > sample2.txt
sed 's|[()]||g' sample2.txt > sample3.txt
Obtained output:
lion:animal
Burger:Food
Not getting the Tiger and an:
Problem : I am not able to get the host and port in same line which is present more then once..i some line it have only one host and port value..in other line there are more than one host and port value..pls help me to slove this ..thank you ..:)
If you want to have all entries in the order of appearance, you can use something like that:
perl -nle 's{Host=([^)]*).*?Port=([^)]*)}{print "$1:$2"}ge' < sample1.txt > sample3.txt