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 4610364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:07:15+00:00 2026-05-22T01:07:15+00:00

I am working on a bash script using sed and cut that will take

  • 0

I am working on a bash script using sed and cut that will take times input in various ways and output them in a specific format. Here is an example line:

timeinhour=$(cut -d" " -f2<<<"$line" | sed 's/p/ /'  | sed 's/a/ /' | sed 's/am/ /' | sed 's/pm/ /' | sed 's/AM/ /' | sed 's/PM/ /' )

As you can see I am just removing any trailing am or pm from a time entry that might be formatted in various ways leaving only the numbers.

So I want this line to just spit out the hour of the day (timeinhour), ie “1000AM” = “10” as does “10a” and “10am.”

The problem I am running into is the varying lengths of the time entries. If I tell sed or cut to remove the last two characters “1000” will correctly output the hour I need: “10,” but using it on one that is already “10” obviously results in a blank output.

I have been experimenting with a line like this

sed 's/\(.*\)../\1/'

If anyone has any advice, I would appreciate it.

For example, this input:

1p
1032AM
419pm
1202a

would produce:

1
10
4
12
  • 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-22T01:07:16+00:00Added an answer on May 22, 2026 at 1:07 am
    sed 's/[^0-9]//g;s/^[0-9]\{1,2\}$/&00/;s/^\(.*\)..$/\1/'
    

    the steps

    1p    -> 1    ->  100 -> 1
    10a   -> 10   -> 1000 -> 10
    419pm -> 419  ->  419 -> 4
    1202a -> 1202 -> 1202 -> 12
    
    • delete what is not number
    • expand 1 or 2 digit (hours) into 4 digit HHmm
    • ignore last two charactes (minutes)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Bash shell script that runs several Python scripts like so:
I am working in a bash script where I need to analyze the output
I'm working with a bash script that is currently working on a server (RHEL4).
I'm trying to make a bash script that will talk to a java program
I am working on a Bash script (see below) that recursively searches through directories
I have the following bash script (.sh) that's working just fine in Unix. I'd
I am working on a bash script where I need to conditionally execute some
I just want to match some text in a Bash script. I've tried using
I'm working on porting bash script to SUS/POSIX-compliant shells. I was able to remove
Working with printf in a bash script, adding no spaces after \n does not

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.