AlecWiese
AlecWiese

Reputation: 43

How to take string from a file name and use it as an argument

If a file name is in this format

assignment_number_username_filename.extension

Ex.

assignment_01_ssaha_homework1.txt

I need to extract just the username to use it in the rest of the script.

How do I take just the username and use it as an argument.

This is close to what I'm looking for but not exactly:

Extracting a string from a file name

if someone could explain how sed works in that scenario that would be just as helpful!

Here's what I have so far; I haven't used cut in a while so I'm getting error messages while trying to refresh myself.

 #!/bin/sh 
 a = $1
 grep $a /home | cut -c 1,2,4,5 echo $a`

Upvotes: 3

Views: 182

Answers (3)

glenn jackman
glenn jackman

Reputation: 246744

Just bash:

filename="assignment_01_ssaha_homework1.txt"
tmp=${filename%_*}
username=${tmp##*_}

http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

Upvotes: 0

Vijay
Vijay

Reputation: 67211

filename="assignment_01_ssaha_homework1.txt"

username=$(echo "$file" | awk -F_ '{print $3}')

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

You probably need command substitution, plus echo plus sed. You need to know that sed regular expressions can remember portions of the match. And you need to know basic regular expressions. In context, this adds up to:

filename="assignment_01_ssaha_homework1.txt"

username=$(echo "$file" | sed 's/^[^_]*_[^_]*_\([^_]*\)_[^.]*\.[^.]*$/\1/')

The $(...) notation is command substitution. The commands in between the parentheses are run and the output is captured as a string. In this case, the string is assigned to the variable username.

In the sed command, the overall command applies a particular substitution (s/match/replace/) operation to each line of input (here, that will be one line). The [^_]* components of the regular expression match a sequence of (zero or more) non-underscores. The \(...\) part remembers the enclosed regex (the third sequence of non-underscores, aka the user name). The switch to [^.]* at the end recognizes the change in delimiter from underscore to dot. The replacement text \1 replaces the entire name with the remembered part of the pattern. In general, you can have several remembered subsections of the pattern. If the file name does not match the pattern, you'll get the input as output.

In bash, there are ways of avoiding the echo; you might well be able to use some of the more esoteric (meaning 'not available in other shells') mechanisms to extract the data. That will work on the majority of modern POSIX-derived shells (Korn, Bash, and others).

Upvotes: 1

Related Questions