Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:08:29+00:00 2026-05-23T04:08:29+00:00

I have to delete every word containing at least one number from each file

  • 0

I have to delete every word containing at least one number from each file given through the command line as parameter. This is my code:

while [ "$*" != "" ]; do
    if [ ! -f $1 ]
        then echo "$1 not file"
    else
        sed -ie  "s/[^ ]*[0-9][^ ]*//g" $1
    fi
    shift
done

It works perfectly if I have only one file, but if I have more it gives the same result for each. After I run the script in each file there will be the result of the first one.

Can someone tell me what I’m missing?

EDIT2:

This is what I’m running now:

while [ "$#" -ne 0 ]; do
for file in "$@"; do
    if [ ! -e "$file" ]; then
        printf "file doesn't exist: %s\n" "$file"
        continue;
    fi
    if [ ! -f "$file" ]; then
        printf "not a file: %s\n" "$file"
        continue;
    fi
done
    sed -i "s/[^ ]*[0-9][^ ]*//g" "$file"
done

I was talking about the done for the while loop and the done for the for loop; but even without that my script keeps running.

EDIT:

Basically the same thing just a bit different. I have to delete the second and fourth word from each line from each file (word only contain alphanumeric characters). Its’ not working properly and I cant find the error. This is my code:

while [ "$*" != "" ]; do
    if [ ! -f $1 ]
        then echo "$1 not file"
    else
        sed -ie  's/^\( *[^ ]+\) +[^ ]+\(.*\)/\1\2/
                  s/^\( *[^ ]+\)\( +[^ ]+\) +[^ ]+\(.*\)/\1\2\3/g' $1
    fi
    shift
done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:08:30+00:00Added an answer on May 23, 2026 at 4:08 am

    the while loop condition should check if there are no arguments, and if there are it should continue. So the right form would be

    while [ "$#" -ne 0 ]; do
    

    Now, what you really want is to get each argument and do something with it. That automatically implies a for loop. So what you really should be doing is

    for file in $@; do
    

    Now, a file can have spaces in it, so getting that filename and checking if it is really a file should be quoted, and you should also check for existance first

    for file in "$@"; do
        if [ ! -e "$file" ]; then
            printf "file doesn't exist: %s\n" "$file"
            continue;
        fi
        if [ ! -f "$file" ]; then
            printf "not a file: %s\n" "$file"
            continue;
        fi
        sed -i "s/[^ ]*[0-9][^ ]*//g" "$file"
    done
    

    I could expand more on sed where the -i switch is restricted only to GNU sed. Apart from that you may also want to keep a backup of that file in case something goes wrong.
    But this is another topic I guess.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have an 'updateinfo' table in order to record every update/insert/delete operations
I have to delete folder with more than 30000 images. I have '.nomedia' file
is there anyway to delete every character on the same line as a cursor,
I have 10 GB of data in fs.chunks and I want to delete every
I am trying to delete every character from the beginning of my string, that
I want to have a batch file that will delete all the folders and
I am writing java/spring webapp in which every user can have its own word
I have to delete an object of an array every now and then, when
I have a delete issue at the moment. We do not have permission to
I have a delete button on a table and a jquery on click eventlistener

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.