I have a very large log file (more than 2GB) and would like to remove all debug logs if they contain 'EntityFramework'.
info: Microsoft.EntityFrameworkCore.Migrations[20405]
No migrations were applied. The database is already up to date.
dbug: Microsoft.EntityFrameworkCore.Infrastructure[10407]
'IDCDbContext' disposed.
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
info: Hangfire.PostgreSql.PostgreSqlStorage[0]
Start installing Hangfire SQL objects...
Here I would like to remove the log below and keep the others
dbug: Microsoft.EntityFrameworkCore.Infrastructure[10407]
'IDCDbContext' disposed.
What I've tried so far:
sed -i '/^dbug/{:b,N;/^[[:lower:]]/!bb};/.*EntityFramework.*/d' logs
However it results in sed: can't find label for jump to b'
Any idea?
This job is better suited for awk:
awk '!p || !/^[[:blank:]]/ {p = /^dbug:/} !p' file
info: Microsoft.EntityFrameworkCore.Migrations[20405]
No migrations were applied. The database is already up to date.
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
info: Hangfire.PostgreSql.PostgreSqlStorage[0]
Start installing Hangfire SQL objects...
We keep a flag p to control whether to print or not. p is set to 1 when a line starts with /dbug:/ and remains set for lines following dbug: that start with whitespaces.
The current error is due to the comma after the b label, there must be a semi-colon. Also, you should include /.*EntityFramework.*/d (or better, /EntityFramework/d) into the command block so that it is executed only inside it:
sed -i '/^dbug/{:b;N;/^[[:lower:]]/!bb;/EntityFramework/d}' logs
See the online demo.
1st solution: With your shown samples, please try following awk program, written and tested in GNU awk. Simple explanation would be, using awk's match function to match regex \ndbug:[^\n]*\n[^\n]* and printing only those lines which are required by OP's shown output(not matched lines only).
awk -v RS= 'match($0,/\ndbug:[^\n]*\n[^\n]*/){print substr($0,1,RSTART-1) substr($0,RSTART+RLENGTH)}' Input_file
2nd solution: Using record separator capability of awk and print the appropriate values needed by OP.
awk -v RS='\ndbug:[^\n]*\n[^\n]*\n' '{gsub(/\n+$/,"")}1' Input_file
Related
This is followup, but slightly different problem to another question that I posted earlier at:
How to extract everything between two patterns (using sed?)?
During testing, I have also found that in some cases, more than one of the records are not only in a single file, but also all the records are just together in a single line, with no newlines between them, and when I run the sed command on that one combined line, it seems like it will find a match, but the match, but it is not the first match (I don't actually know why it is matching the one that it is finding).
I know that the first "line" is also the most recent record, so I would like to perform a sed type match to extract the time from the first record, but only from the first record.
Here a snippet to illustrate what I tried to describe above:
UPDATE SUCCESS (key/name): 7c061313-9cbd-4bd1-8613-139cbd2bd1da/snctest38
UPDATE SUCCESS (key/name): 7ebab268-05dc-4110-bab2-6805dc2110f6/snctest4
UPDATE SUCCESS (key/name): 8ef15385-3d23-40e9-b153-853d2310e9fd/snctest5
UPDATE SUCCESS (key/name): 978bf8b5-b4aa-4cd4-8bf8-b5b4aaacd4b6/snctest6
UPDATE SUCCESS (key/name): 34f28da7-1311-4cb7-b28d-a713116cb77d/snctest7
UPDATE SUCCESS (key/name): 7be77991-4c73-4c35-a779-914c73ec359a/snctest8
</message><refDesc>PUSH Task 0a64cff0-b826-420b-a4cf-f0b826420bae 2_ldapadd</refDesc><refKey>0a64cff0-b826-420b-a4cf-f0b826420bae</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:59.649Z</end><start>2020-01-22T05:36:59.639Z</start><jobType>TASK</jobType><key>c4a0f6fa-109e-4a25-a0f6-fa109e5a2505</key><refDesc>PROPAGATION Task 3a2457ef-556b-4f39-a457-ef556b6f3921 d228de62-2918-46fe-960a-a193c3bfa0c3</refDesc><refKey>3a2457ef-556b-4f39-a457-ef556b6f3921</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:59.567Z</end><start>2020-01-22T05:36:59.558Z</start><jobType>TASK</jobType><key>e9defd1b-966f-4830-9efd-1b966f183008</key><refDesc>PROPAGATION Task a47fee5a-fea3-4422-bfee-5afea3e422c5 e7394424-a4b7-41fe-90a1-46e3b309e6f2</refDesc><refKey>a47fee5a-fea3-4422-bfee-5afea3e422c5</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:59.485Z</end><start>2020-01-22T05:36:59.476Z</start><jobType>TASK</jobType><key>52ab4c49-e28d-49d3-ab4c-49e28d39d38e</key><refDesc>PROPAGATION Task 437e3eb4-487f-471c-be3e-b4487f971cdf 11f95ec1-ac16-4344-8741-ae362ec4b357</refDesc><refKey>437e3eb4-487f-471c-be3e-b4487f971cdf</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:59.394Z</end><start>2020-01-22T05:36:59.385Z</start><jobType>TASK</jobType><key>17c947c9-eb8d-4222-8947-c9eb8d622288</key><refDesc>PROPAGATION Task 67956b5a-86a2-4f0c-956b-5a86a24f0c35 d7df1a03-a7f7-4a7b-8cab-d098852de2f3</refDesc><refKey>67956b5a-86a2-4f0c-956b-5a86a24f0c35</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:59.312Z</end><start>2020-01-22T05:36:59.302Z</start><jobType>TASK</jobType><key>42a7e05a-5ed2-438c-a7e0-5a5ed2e38c64</key><refDesc>PROPAGATION Task d3d6026d-f4fd-4c64-9602-6df4fd1c6455 321861ed-3ede-4981-a6cc-cc193662a652</refDesc><refKey>d3d6026d-f4fd-4c64-9602-6df4fd1c6455</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:59.226Z</end><start>2020-01-22T05:36:59.216Z</start><jobType>TASK</jobType><key>09e1767b-e243-47ea-a176-7be243a7eabd</key><refDesc>PROPAGATION Task 3dd00b2e-e0ab-43c8-900b-2ee0ab43c8de a3b19123-38de-4702-907a-11d7a14d7a64</refDesc><refKey>3dd00b2e-e0ab-43c8-900b-2ee0ab43c8de</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:59.145Z</end><start>2020-01-22T05:36:59.135Z</start><jobType>TASK</jobType><key>b13e32e2-e361-4175-be32-e2e361917501</key><refDesc>PROPAGATION Task de139a01-fb49-4ea5-939a-01fb49cea584 2a6b616b-5319-423c-9d4a-47157dfe2cbc</refDesc><refKey>de139a01-fb49-4ea5-939a-01fb49cea584</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:59.064Z</end><start>2020-01-22T05:36:59.054Z</start><jobType>TASK</jobType><key>cb24ebcd-6448-444b-a4eb-cd6448944bbc</key><refDesc>PROPAGATION Task aa947230-0cb2-40d2-9472-300cb270d230 0fade8e2-cfdf-4dc4-87ba-6d1352d32840</refDesc><refKey>aa947230-0cb2-40d2-9472-300cb270d230</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.983Z</end><start>2020-01-22T05:36:58.973Z</start><jobType>TASK</jobType><key>04897743-9db6-496f-8977-439db6596fab</key><refDesc>PROPAGATION Task bc3ae42c-701e-4258-bae4-2c701e3258cc 98f9e4de-0e86-447d-9ffa-c87c96a947ca</refDesc><refKey>bc3ae42c-701e-4258-bae4-2c701e3258cc</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.901Z</end><start>2020-01-22T05:36:58.891Z</start><jobType>TASK</jobType><key>999bab8f-9e64-4f53-9bab-8f9e64df5320</key><refDesc>PROPAGATION Task 15371c79-1ac0-4282-b71c-791ac0e28230 fbdf2574-5e7d-4e97-af94-e4d6169fc02b</refDesc><refKey>15371c79-1ac0-4282-b71c-791ac0e28230</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.819Z</end><start>2020-01-22T05:36:58.810Z</start><jobType>TASK</jobType><key>81e907f6-d358-42b9-a907-f6d358e2b997</key><refDesc>PROPAGATION Task 5401a5d8-0407-4c2f-81a5-d804071c2fd0 81679e82-7006-4a2b-8914-f76fea587cec</refDesc><refKey>5401a5d8-0407-4c2f-81a5-d804071c2fd0</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.738Z</end><start>2020-01-22T05:36:58.728Z</start><jobType>TASK</jobType><key>a6b42457-4b18-4dcc-b424-574b180dcc51</key><refDesc>PROPAGATION Task 90cc1ef6-5b5a-4f4d-8c1e-f65b5acf4d16 42ac4e4e-71e8-4cfb-8c2e-ceced4cdf029</refDesc><refKey>90cc1ef6-5b5a-4f4d-8c1e-f65b5acf4d16</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.655Z</end><start>2020-01-22T05:36:58.645Z</start><jobType>TASK</jobType><key>8aba0dc9-3df4-4398-ba0d-c93df453989c</key><refDesc>PROPAGATION Task a074cbf2-de6c-4c15-b4cb-f2de6c4c15cd 6e37c044-5aa2-45e6-9626-27e0b2f52038</refDesc><refKey>a074cbf2-de6c-4c15-b4cb-f2de6c4c15cd</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.571Z</end><start>2020-01-22T05:36:58.561Z</start><jobType>TASK</jobType><key>723f4400-49cd-4507-bf44-0049cdd507a1</key><refDesc>PROPAGATION Task cb87d842-2849-45ca-87d8-422849e5caa8 8bcd6de0-9e6f-4261-89b3-e3d56400f7af</refDesc><refKey>cb87d842-2849-45ca-87d8-422849e5caa8</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.485Z</end><start>2020-01-22T05:36:58.474Z</start><jobType>TASK</jobType><key>a0b82ca0-8e2e-4b05-b82c-a08e2e8b0548</key><refDesc>PROPAGATION Task 828178dd-c1c8-4668-8178-ddc1c80668e7 d91141d7-2fd4-4fef-adc4-79cb81f1560b</refDesc><refKey>828178dd-c1c8-4668-8178-ddc1c80668e7</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.401Z</end><start>2020-01-22T05:36:58.391Z</start><jobType>TASK</jobType><key>0c2f6210-f095-427c-af62-10f095927c09</key><refDesc>PROPAGATION Task 1b9cb7c6-7b51-4f4a-9cb7-c67b51bf4ae9 440fa8f9-ecd4-4053-9b07-fd22ed49b139</refDesc><refKey>1b9cb7c6-7b51-4f4a-9cb7-c67b51bf4ae9</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.319Z</end><start>2020-01-22T05:36:58.309Z</start><jobType>TASK</jobType><key>77b14d6c-5be6-40e0-b14d-6c5be6f0e00b</key><refDesc>PROPAGATION Task 289a014d-3646-456d-9a01-4d3646356dd0 59fd7993-2737-46d6-8c0e-a26f8f02e234</refDesc><refKey>289a014d-3646-456d-9a01-4d3646356dd0</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.237Z</end><start>2020-01-22T05:36:58.227Z</start><jobType>TASK</jobType><key>0f59fe8b-f057-4039-99fe-8bf057d0394e</key><refDesc>PROPAGATION Task 3f9f7751-787f-4dd2-9f77-51787fedd2bf 106abcb7-a5f0-4333-8e9e-0a81e84af904</refDesc><refKey>3f9f7751-787f-4dd2-9f77-51787fedd2bf</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.154Z</end><start>2020-01-22T05:36:58.143Z</start><jobType>TASK</jobType><key>a5c44fb7-2b39-4e22-844f-b72b39de22ff</key><refDesc>PROPAGATION Task d9f712ea-d3a7-44a0-b712-ead3a784a05e efb1c4ca-6598-4cce-9aef-c749bf18e10b</refDesc><refKey>d9f712ea-d3a7-44a0-b712-ead3a784a05e</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:58.066Z</end><start>2020-01-22T05:36:58.056Z</start><jobType>TASK</jobType><key>a9c2b65a-2cc6-4e70-82b6-5a2cc6fe7083</key><refDesc>PROPAGATION Task e504ad04-6afc-4880-84ad-046afcd8806c a025af4f-b7b8-4d90-917c-d3ff79d13780</refDesc><refKey>e504ad04-6afc-4880-84ad-046afcd8806c</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:57.985Z</end><start>2020-01-22T05:36:57.975Z</start><jobType>TASK</jobType><key>a433e0da-71e5-4957-b3e0-da71e5d95744</key><refDesc>PROPAGATION Task 30597ec2-8cea-4744-997e-c28cea47446a 101d544c-5355-436b-9e93-aebcbf3b5da4</refDesc><refKey>30597ec2-8cea-4744-997e-c28cea47446a</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:57.902Z</end><start>2020-01-22T05:36:57.891Z</start><jobType>TASK</jobType><key>a854c35a-291f-43c8-94c3-5a291fb3c891</key><refDesc>PROPAGATION Task 58ed721f-0b25-4a39-ad72-1f0b255a39c6 404aa838-b14a-40fd-8e6f-5a0522cfcce8</refDesc><refKey>58ed721f-0b25-4a39-ad72-1f0b255a39c6</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:57.817Z</end><start>2020-01-22T05:36:57.807Z</start><jobType>TASK</jobType><key>19a4823a-b57e-4a51-a482-3ab57eaa5172</key><refDesc>PROPAGATION Task 9d0c6898-185b-4eac-8c68-98185b9eacb8 2b6dc9a1-8cf9-4b8c-a597-b1fc4ecd9b72</refDesc><refKey>9d0c6898-185b-4eac-8c68-98185b9eacb8</refKey><status>SUCCESS</status></syncope21:exec><syncope21:exec xmlns:syncope21="http://syncope.apache.org/2.1"><end>2020-01-22T05:36:57.728Z</end><start>2020-01-22T05:36:57.717Z</start><jobType>TASK</jobType><key>f5b0ccb5-304f-40d2-b0cc-b5304f30d21e</key><refDesc>PROPAGATION Task 98b26cc8-0ff9-487b-b26c-c80ff9487b59 ac164590-3e66-42a5-b7d7-c001fa641a24</refDesc><refKey>98b26cc8-0ff9-487b-b26c-c80ff9487b59</refKey><status>SUCCESS</status></syncope21:exec></syncope21:execs>
As you can see there are apparently multiple "lines" glommed together into one large line.
So how can some of the sed commands that were suggested in that previous thread be modified to match and extract just the first match?
Thanks,
Jim
If I'm reading your question right (and this is not easy given its dependence on other questions), you just want the shorted match between two given regexes (which are themselves just strings): <\/messages> and <\/start>.
sed '/<\/message>.*<\/start>/ { s:^.*</message>:</message>:; s:</start>.*$:</start>: }'
This uses POSIX sed since you have not specified that you're using GNU (e.g. via a Linux distribution). First, we check to ensure the line has a match. If it does, remove everything before the final </message> and then remove everything after the first </start>.
This would be more elegant with GNU (or BSD) sed since you can use backreferences to retain those tags:
sed -E '/<\/message>.*<\/start>/ { s:^.*(</message>):\1:; s:(</start>).*$:\1: }'
Personally, I'd use GNU grep (assuming it's compiled with libpcre to support -P):
grep -Po '</message>.*?</start>'
This will only display the matching text of the shortest match between those two given strings.
I think I have uncovered an error in grep. If I run this grep statement against a db log on the command line it runs fine.
grep "Query Executed in [[:digit:]]\{5\}.\?" db.log
I get this result:
Query Executed in 19699.188 ms;"select distinct * from /xyztable.....
when I run it in a script
LONG_QUERY=`grep "Query Executed in [[:digit:]]\{5\}.\?" db.log`
the asterisk in the result is replaced with a list of all files in the current directory.
echo $LONG_QUERY
Result:
Query Executed in 19699.188 ms; "select distinct <list of files in
current directory> from /xyztable.....
Has anyone seen this behavior?
This is not an error in grep. This is an error in your understanding of how scripts are interpreted.
If I write in a script:
echo *
I will get a list of filenames, because an unquoted, unescaped, asterisk is interpreted by the shell (not grep, but /bin/bash or /bin/sh or whatever shell you use) as a request to substitute filenames matching the pattern '*', which is to say all of them.
If I write in a script:
echo "*"
I will get a single '*', because it was in a quoted string.
If I write:
STAR="*"
echo $STAR
I will get filenames again, because I quoted the star while assigning it to a variable, but then when I substituted the variable into the command it became unquoted.
If I write:
STAR="*"
echo "$STAR"
I will get a single star, because double-quotes allow variable interpolation.
You are using backquotes - that is, ` characters - around a command. That captures the output of the command into a variable.
I would suggest that if you are going to be echoing the results of the command, and little else, you should just redirect the results into a file. (After all, what are you going to do when your LONG_QUERY contains 10,000 lines of output because your log file got really full?)
Barring that, at the very least do echo "$LONG_QUERY" (in double quotes).
I am very new to shell scripting and trying to learn the "sed" command functionality.
I have a file called configurations.txt with some variables defined in it with some string values initialised to each of them.
I am trying to replace a string in a file (values.txt) which is present in some other directory by the values of the variables defined. The name of the file is values.txt.
Data present in configurations.txt:-
mem="cpu.memory=4G"
proc="cpu.processor=Intel"
Data present in the values.txt (present in /home/cpu/script):-
cpu.memory=1G
cpu.processor=Dell
I am trying to make a shell script called repl.sh and I dont have alot of code in it for now but here is what I got:-
#!/bin/bash
source /home/configurations.txt
sed <need some help here>
Expected output is after an appropriate regex applied, when I run script sh repl.sh, in my values.txt , It must have the following data present:-
cpu.memory=4G
cpu.processor=Intell
Originally which was 1G and Dell.
Would highly appreciate some quick help. Thanks
This question lacks some sort of abstract routine and looks like "help me do something concrete please". Thus it's very unlikely that anyone would provide a full solution for that problem.
What you should do try to split this task into number of small pieces.
1) Iterate over configuration.txt and get values from each line. To do that you need to get X and Y from a value="X=Y" string.
This regex could be helpful here - ([^=]+)=\"([^=]+)=([^=]+)\". It contains 3 matching groups separated by ". For example,
>> sed -r 's/([^=]+)=\"([^=]+)=([^=]+)\"/\1/' configurations.txt
mem
proc
>> sed -r 's/([^=]+)=\"([^=]+)=([^=]+)\"/\2/' configurations.txt
cpu.memory
cpu.processor
>> sed -r 's/([^=]+)=\"([^=]+)=([^=]+)\"/\3/' configurations.txt
4G
Intel
2) For each X and Y find X=Z in values.txt and substitute it with a X=Y.
For example, let's change cpu.memory value in values.txt with 4G:
>> X=cpu.memory; Y=4G; sed -r "s/(${X}=).*/\1${Y}/" values.txt
cpu.memory=4G
cpu.processor=Dell
Use -i flag to do changes in place.
Here is an awk based answer:
$ cat config.txt
cpu.memory=4G
cpu.processor=Intel
$ cat values.txt
cpu.memory=1G
cpu.processor=Dell
cpu.speed=4GHz
$ awk -F= 'FNR==NR{a[$1]=$2; next;}; {if($1 in a){$2=a[$1]}}1' OFS== config.txt values.txt
cpu.memory=4G
cpu.processor=Intel
cpu.speed=4GHz
Explanation: First read config.txt & save in memory. Then read values.txt. If a particular value was defined in config.txt, use the saved value from memory (config.txt).
I'm trying to write a script which will analyse a log file,
i want to give the user the option to enter a pattern and then print any line which matches this pattern in a specific column (the fifth one)
the following works from the terminal
awk ' $5=="acpid:" {print$0}' *filename*
ok so above im trying to match "acpid:" this works fine but in the script i want to be able to allow multiple entries and search for them all, the problem is i'm messing up the variable in the script this is what i have:
echo "enter any services you want details on, seperated by spaces"
read -a details
for i in ${details[#]}
do
echo $i
awk '$5 == "${i}" {print $0}' ${FILE}
done
again if i directly put in a matching expression instead of the variable it works so i guess my problem is here any tips would be great
UPDATE
So im using the second option suggested(shown below) by #ghoti as it matches my log file slightly better
however im not having any luck with multiple entries. ive added two lines to illustratre the results im getting these are echo $i and echo "finish loop" as placed they should tell me what input the loop is currently on and that im leaving the loop
'read -a details
re=""
for i in "${details[#]}"; do
re="$re${re:+|}$i"
echo $i
echo"finish loop"
done
awk -v re="$re" '$5 ~ re' "$FILE" `
When i give read an input of either "acpid" or "init" seperately a perfect result is matched, however when the input is "acpid init" the following is the output
acpid init
finish loop
What im seeing from this is that the read is taking the both words as one entry and then the awk is searching but not matching them (as would be expected). so why is the input not being taken as two separate entries i had thought the -a option with read specified that words separated by a space would be placed into separate elements of the array. perhaps i have not declared the array correctly?
Update update
ok cancel the above update like i fool i'd forgotten that id chaged IFS to \n earlier in the script changed it back and bingo !!!
Many thanks again to #ghoti for his help!!
There are a few ways that you could do what you want.
One option might be to run through a for loop for each word, then apply a different call to awk, and show the results sequentially. For example, if you entered foo bar into the $details variable, you might get a list of foo matches, followed by a list of bar matches:
read -a details
for i in "${details[#]}"; do
awk -v s="$i" '$5 == s' "$FILE"
done
The idea here is that we use awk's -v option to get each word into the script, rather than expanding the variable inside the quoted script. You should read about how bash deals with different kinds of quotes. (There are also a few Stackoverflow questions on the topic, here and here and elsewhere.)
Another option might be to construct a regular expression that searches for all the words you're interested in, all at once. This has the benefit of using a single run of awk to search through $FILE:
read -a details
re=""
for i in "${details[#]}"; do
re="$re${re:+|}$i"
done
awk -v re="$re" '$5 ~ re' "$FILE"
The result will contain all the interesting lines from $FILE in the order in which they appear in $FILE, rather than ordered by the words you provided.
Note that this is a fairly rudimentary search, without word boundaries, so if you search for "foo bar babar", you may get results you don't want. You can play with the regex yourself, though. :)
Does that answer your question?
my first post here and beginner level. Is there a way I can solve this problem with sed (or any other means)? I want to manipulate a newly created file daily and replace some IP and port occurences.
1) I want to replace the first occurence of "5027,5028" with A3 and the second with A4.
2) I want to replace the first occurence of "5026" with A1 and the second with A2.
PS. I have tried to simplify the example and left the preceeding lines with version="y" or version="x" that could be of help to distinguish the occurences from eachother. (The first x and y version pair is a primary connection and the other two the secondary connection).
Input file:
version="x"
commaSeparatedList="5027,5028"`
version="y"
commaSeparatedList="5026"
version="x"
commaSeparatedList="5027,5028"
version="y"
commaSeparatedList="5026"
Edited file:
version="1.4.1-12"
commaSeparatedList="A3"
version="1.3.0"
commaSeparatedList="A1"
version="1.4.1-12"
commaSeparatedList="A4"
version="1.3.0"
commaSeparatedList="A2"
Sorry, I had some editing horror for a few minutes. Hope it looks easier to understand now. I am basically receiving this file on a system that is deployed nightly and I want to edit this file using a cron job before it starts to make sure a connection works.
Do not bother trying to use sed for this. It can be done, but sed is the wrong tool.
Use awk instead. To replace the first occurrence of "5027,5028" with A3 and the second with A4.
awk '/5027,5028/ && count < 2 { if( count ++ ) repl="A4"; else repl="A3";
sub( "5027,5028", repl)} 1' input
The second replacement is left as an exercise. It is basically the same thing, and you can either run awk twice or just add additional clauses the above.
To overwrite the original file, use shell redirections:
awk ... input > tmpfile && mv tmpfile input
This might work for you (GNU Sed):
sed '1,/5027,5028/s/5027,5028/A3/;s/5027,5028/A4/;1,/5026/s/5026/A1/;s/5026/A2/' file