CodexBloom - Programming Q&A Platform

Unexpected output when using 'awk' to process multiline strings in bash

๐Ÿ‘€ Views: 16 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-19
bash awk scripting

I can't seem to get I'm trying to configure I tried several approaches but none seem to work. I'm experimenting with Hey everyone, I'm running into an issue that's driving me crazy... I'm working on a personal project and I'm trying to parse multiline strings in a bash script using `awk`, but I'm working with unexpected output. The goal is to extract specific lines from a block of text, but it seems like `awk` is not handling the newlines correctly. Hereโ€™s the relevant code snippet: ```bash #!/bin/bash input=""" Line 1: Start Line 2: Continue Line 3: End """ # Attempting to echo the entire input echo "Processing input..." # This awk command is supposed to filter lines containing 'Continue' result=$(echo "$input" | awk '/Continue/ {print}') echo "Result: $result" ``` When I run this script, I get the output: ``` Result: ``` It seems like `awk` isn't recognizing the multiline input correctly. Iโ€™ve tried wrapping the input in double quotes and single quotes but it hasnโ€™t made a difference. My `awk` version is 4.1.4 on Ubuntu 20.04. I also attempted to use `printf` instead of `echo`, thinking it might handle the input better, but the scenario continues. Is there a specific way I should format the multiline string or use `awk` to ensure it processes the input as expected? Thanks in advance! For reference, this is a production mobile app. I've been using Bash for about a year now. Any feedback is welcome! I recently upgraded to Bash 3.11. The project is a desktop app built with Bash. What's the best practice here?