LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Bash script to edit text file (https://www.linuxquestions.org/questions/linux-desktop-74/bash-script-to-edit-text-file-518072/)

snowman81 01-10-2007 12:40 PM

Bash script to edit text file
 
I have a text file that is too big to edit by hand and I want to create a simple little script that deletes part of the entry and leaves the rest.

For example, every line has something like this:

Code:

http://user:pass@whatever.whatever.com/
I want to only have
Code:

user:pass
Can anyone help?

FnordPerfect 01-10-2007 12:57 PM

This is not very smart, but might do the trick:

Code:

#!/bin/sh

if [ -z $1 ]; then
        echo "Needs filename as argument."
        exit 1
elif [ -f "$1" ]; then
        fname="$1"
fi

while read a;
do

  echo "$a" | sed -e "s+http://++" -e "s+@.*$++g"

done < "$fname"

* Save this as a shell script of a name you like, e.g. url-extract.sh

* Make it executable with chmod +x url-extract.sh

* Give your filename as the first argument:
url-extract.sh filename_that_is_to_big_to_edit_by_hand.txt

and it returns only user:pass as you wanted

snowman81 01-10-2007 03:33 PM

Ok, I shall give it a shot.


Sweet, it worked.


All times are GMT -5. The time now is 03:17 PM.