Score:1

check a condition for match string in for loop

eg flag

I have a text file and wrote a bash script to loop over every line of it and if its 6th column match a condition I want to modify its line.

How can I match it condition that if 6th = string? My bash:

#!/bin/bash
IFS=$'\n'
FILENAME="/file"
LINES=$(cat $FILENAME)
        
for LINE in $LINES; do
if [?]
then
"SOME ACTION"
fi
done

my file:

"ICMP-S0-Dx.x.x.x/24", "x", x, "any", "any", icmp, 0.0.0.0/0, x.x.x.x/24, {3000, drop, -1, fast+dsample:1000}, {300k, alert, -1, none}
hr flag
What *exactly* is the intended result? a shell loop is likely not the best way to do this kind of thing (and a `for` loop is the worst choice of loop construct here). See for example [Why is using a shell loop to process text considered bad practice?](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice)
masoud hanifehzadeh avatar
eg flag
I want to change second IP address from /24 to /23 via ipcalc in line that has icmp as its protocol. Also I want to change the name in first field as well.
hr flag
So I'd suggest you use a tool like awk, perl or miller - something like `awk -F', ' 'BEGIN{OFS=FS} $6 == "icmp" {$1 = "something else"} 1' file`
masoud hanifehzadeh avatar
eg flag
How can I use this in If condition? Beside my method is there a better way to implement this scenario?
hr flag
In the context of an awk script, `$6 == "icmp"` effectively *is* an "if" condition - see for example [Getting Started with awk](https://www.gnu.org/software/gawk/manual/gawk.html#Getting-Started)
Score:1
in flag

if [ $(echo "$LINE" | cut -d, -f6) = SomeString ]

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.