Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Shell Extract Text Before Digits in a String

Course Queries Syllabus Queries
Max. 2000 characters
Replies

usr_profile.png
Liza Sain

User

( 6 months ago )

 

I've found several examples of extractions before a single character and examples of extracting numbers, but I haven't found anything about extracting characters before numbers.

My question: Some of the strings I have look like this:

NUC320 Syllabus Template - 8wk
SLA School Template - UL
CJ101 Syllabus Template - 8wk
TECH201 Syllabus Template - 8wk
Test Clone ID17

In cases where the string doesn't contain the data I want, I need it to be skipped. The desired output would be:

NUC-320
CJ-101
TECH-201

SLA School Template - UL & Test Clone ID17 would be skipped.

I imagine the process being something to the effect of:

  1. Extract text before " "
  2. Condition - Check for digits in the string
  3. Extract text before digits and assign it to a variable x
  4. Extract digits and assign to a variable y
  5. Concatenate $x"-"$y and assign to another variable z

More information: The strings are extracted from a line in a couple thousand text docs using a loop. They will be used to append to a hyperlink and rename a file during the loop.

Edit:

    #!/bin/sh

    # my files are named 1.txt through 9999.txt i both 
    # increments the loop and sets the filename to be searched

    i=1

    while [ $i -lt 10000 ]
    do
        x=$(head -n 31 $i.txt | tail -1 | cut -c 7-)
        if [ ! -z "$x" -a "$x" != " " ]; then
# I'd like to insert the hyperlink with the output on the
# same line (1.txt;cj101 Syllabus Template - 8wk;www.link.com/cj101)
            echo "$i.txt;$x" >> syllabus.txt
    #   else
    #       rm $i.txt
        fi
        i=`expr $i + 1`
        sleep .1
    done

usr_profile.png
Charles Kyobe

User

( 6 months ago )

sed for printing lines starting with capital letters followed by digits. It also adds a - between them:

sed -n 's/^\([A-Z]\+\)\([0-9]\+\) .*/\1-\2/p' input 

Gives:

NUC-320
CJ-101
TECH-201

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif