Posts

Showing posts with the label Shell-script

How to remove entire string that match specific pattern in unix txt file with a single command line

 You can remove entire lines in a text file that match a specific pattern using `grep` with the `-v` option to exclude matching lines. Here's an example using `grep` to remove lines containing a specific pattern from a text file: ```bash grep -v "pattern" input.txt > output.txt ``` Replace `"pattern"` with the specific pattern you want to match, `input.txt` with the name of your input file, and `output.txt` with the name of the file where you want to save the result. For example, if you want to remove lines containing the word "example" from a file called `data.txt` and save the result to a new file called `filtered_data.txt`, you can use the following command: ```bash grep -v "example" data.txt > filtered_data.txt ``` This will create a new file `filtered_data.txt` that contains all the lines from `data.txt` except the ones containing the word "example."

How to get the json data used in each curl statement while using xargs and maps it to its corresponding result?

 To get the JSON data used in each `curl` statement while using `xargs` and map it to its corresponding result, you can use a combination of `xargs` and `jq`, a lightweight and flexible command-line JSON processor. Here's a step-by-step guide: Assuming you have a file containing a list of JSON data (one JSON object per line) and you want to make a `curl` request with each JSON object and then map the response to the original input: 1. **Create a file with JSON data**:    You should have a file (e.g., `input.json`) with one JSON object per line. For example:    ```json    {"id": 1, "name": "John"}    {"id": 2, "name": "Jane"}    {"id": 3, "name": "Bob"}    ``` 2. **Use `xargs` with `curl` and `jq`**:    You can use the `xargs` command along with `curl` and `jq` to make the `curl` request for each line of JSON data and then process the response. Here's a command that does this:    ```bash  

Handling user input of * with case in shell script

Question about handling user input with `case` in a shell script. Using the `case` statement is an efficient way to handle different user inputs in a shell script, especially when you have multiple options to manage. Here's how you can use `case` to handle user input in a shell script: ```bash #!/bin/bash # Display a menu to the user echo "Select an option:" echo "1. Option 1" echo "2. Option 2" echo "3. Option 3" echo "Q. Quit" # Read user input read choice # Use case to handle the user's choice case "$choice" in   1)     echo "You selected Option 1"     # Perform actions for Option 1     ;;   2)     echo "You selected Option 2"     # Perform actions for Option 2     ;;   3)     echo "You selected Option 3"     # Perform actions for Option 3     ;;   [qQ])     echo "Goodbye!"     # Optionally, you can include actions to quit the script here     ;;   *)     echo "Invalid cho