LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2022, 02:28 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
bash script for grep with context


Am writing a bash script that uses grep to search phrases and prints them on terminal. I want the user to have the possibility of using the context options of grep (-C, -A, -B). I made an indexed array ICTX, which I want to use for making the context arguments to grep (-C NUM, -A NUM, -B NUM, -A NUM1 -B NUM2).

Would help having some advice on how I can neatly handle the making of ICTX with tho code listed below.

Code:
  while (( $# > 0 )); do
   case "$1" in
     ("-C"|"--context")
         ctx="$2" ; shift ; shift ;;
    
     ("--Bfc"|"--before-context")
         bfctx="$2" ; shift ; shift ;;

     ("--Afc"|"--after-context")
         afctx="$2" ; shift ; shift ;;
   esac
  done

  [[ -z "$ctx" ]] && ctx=8
  ictx=( -C "$ctx" )
  ictx=( -A "$afctx" )
  ictx=( -B "$bfctx" )

  grep -ni "${ictx[@]}" -e "$ptrn" -- "$fl"

Last edited by Faki; 05-16-2022 at 03:42 PM.
 
Old 05-16-2022, 03:20 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
$# is a special variable in bash, that expands to the number of arguments

a.sh
Code:
#!/usr/bin/bash
echo "$#"
Code:
bash -c 'echo $#'
0

bash -c 'echo $#' - A
1

bash -c 'echo $#' - A B
2

bash ./a.sh foo bar
2
Now, to the problem I see.
If ctx is zero length then ctx = 8
Code:
[[ -z "$ctx" ]] && ctx=8

echo "$ctx"
8

ictx=( -C "$ctx" )
echo "$ictx"
-C

ictx="-C "$ctx""
echo "$ictx"
-C 8
You also try and set the same variable 3 times
Code:
ictx=( -C "$ctx" )
ictx=( -A "$afctx" )
ictx=( -B "$bfctx" )
Code:
a=1
a=2
a=3
echo "$a"
3
All that I can tell you without seeing more.
 
Old 05-16-2022, 03:47 PM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Yes, I am setting the same ICTX array three times. It is currently the problem I am discussing in the question. The question is about constructing the context array ICTX in a way that does not produce conflicts (e.g. using -C with -A or -B). By default I want to use -C 8 when no options are supplied by the user (i.e. no -C, -B or -A).
 
Old 05-16-2022, 03:58 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,716

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Code:
  ictx=( -C "$ctx" )
  ictx=( -A "$afctx" )
  ictx=( -B "$bfctx" )
Each line initializes the array so ictx will only contain the values -B $bfctx regardless. Instead of an array you can just concatenate the values into a single string but If any of the other options i.e afctx or bfctx are empty then that will probably cause an error.

Quote:
astring="-C $ctx -A $afctx -B $bfctx"
 
Old 05-16-2022, 04:01 PM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Not sure If I know what you want.

Example:
Code:
ctx="one"
afctx="two"
bfctx="three"

declare -A MyArray=( [C]="$ctx" [A]="$afctx" [B]="$bfctx" )

echo "${!MyArray[@]}"
echo "${MyArray[@]}"

echo ${MyArray[C]}
 
Old 05-16-2022, 04:52 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,716

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
May not be the best..
Code:
if  [[ $# == 0 ]]; then
    grep -ni "-C 8" -e "$ptrn" -- "$fl"
    exit
fi

  while (( $# > 0 )); do
   case "$1" in
     ("-C"|"--context")
         ctx="$2" ; shift ; shift ;;
    
     ("--Bfc"|"--before-context")
         bfctx="$2" ; shift ; shift ;;

     ("--Afc"|"--after-context")
         afctx="$2" ; shift ; shift ;;
   esac
  done
I do not know of a better way then testing each parameter to see if it is empty and constructing your grep string in the desired order.
 
Old 05-16-2022, 05:09 PM   #7
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I have done as follows as an initial try. Could you criticise please?

Code:
ictx=()
[ -n "$bfctx" ] && ictx+=( -B "$bfctx" )
[ -n "$afctx" ] && ictx+=( -A "$afctx" )

[[ -z "$ctx" ]] && ctx=8
(( ${#ictx[@]} == 0 )) && ictx+=( -C "$ctx" )

Last edited by Faki; 05-16-2022 at 05:10 PM.
 
Old 05-19-2022, 11:38 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See this page https://wiki.bash-hackers.org/syntax..._default_value ; see subsection re default value setting.
 
  


Reply

Tags
grep



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep with context danielbmartin Programming 9 04-25-2015 07:49 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
[bash-script] A question about using grep in the script thomas2004ch Linux - Software 2 03-05-2012 03:27 AM
[SOLVED] Bash script - how to use output from diff and find in context with cpio Mogget Programming 4 01-23-2009 10:38 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration