Shell Script Help on file reading


Recommended Posts

Hi,

I need a little help on file reading. I have a file and its contents are like

123456789123456789123456789

987654321987654321987654321

231297301283120381290382133

Now I need script to format this file like

A B C D E F G H I J K L M

123 456 789 123 456 789 12 3 4 56 7 8 9

987 654 321 987 654 321 98 7 6 54 3 2 1

231 297 301 283 120 381 29 0 3 82 1 3 3

i.e the first line shows the differentiating columns and the rest of the lines following those columns.

Please help me on how this can be done.

Thank you.

Link to comment
https://www.neowin.net/forum/topic/1136228-shell-script-help-on-file-reading/
Share on other sites

Is the break down the same,

3-digits 3-digits 3-digits 3-digits 3-digits 3-digits 2-digits 1-digit 1-digit 2-digits 1-digit 1-digit?

This might be an awkward way about the problem but,

#! /bin/bash

for i in `cat filename.ext`;

do

echo ${i:0:2} ${i:3:6} ${i:7:9} ${i:10:12} ${i:13:15} ${i:16:18} ${i:19:20} ${i:21:21} ${i:22:22} ${i:23:24} ${i:25:25} ${i:26:26} >> outputfiles.ext

done;

Clearly not the cleanest way of doing it - P.s. I have not tested this as I am on an android phone lol

You are very close. This is exactly what I want. In simple terms, I want to break the characters and present them in a format like first three characters. then 4-6. then 7-12, then 8-9 and so on. The length of each line in the file remains the same and so I need to format it in different ways.

I tested your code, but the output is not coming according to the code. It would have been perfect otherwise.

Thank you :)

I tested your code, but the output is not coming according to the code. It would have been perfect otherwise.

Do you mean,

1: The wrong number of digits broke down? (Which is possible as I wrote that on an Andriod phone and I cannot see the whole block of code.)

2: The IFS the internal field separate is not triggering the line breaks? (Which is possible depending on where and what is used to create the file in the first place)

This is coming as output of your code

12 456789 891234567 23456789 56789 89

98 654321 219876543 87654321 54321 21

12 121112 123123124 12312412 12412 12

This is not consistent with the code. Your code has divided each line into 12 parts. Therefore the output should have been in 12 parts. The file has been created just by typing the lines to check how it can be broken up.

I would be interested in knowing what this is for LOL

I did the example from memory and I made a mistake in thinking is Variable:Start Reference:End Reference where in actuality it is Variable:Start Reference:Length

#

# YOUR EXAMPLE

#

[Tim.IT-8] ? cat example.txt

123456789123456789123456789

987654321987654321987654321

231297301283120381290382133

#

# SCRIPT

#

[Tim.IT-8] ? cat example.sh

#! /bin/bash

# VARIABLES

INPUTFILE=example.txt

OUTPUTFILE=example_completed.txt

# CLEAR THE SCREEN

clear

# PROCESS

for i in `cat $INPUTFILE`;

do

echo "${i:0:3} ${i:3:3} ${i:6:3} ${i:9:3} ${i:12:3} ${i:15:3} ${i:18:2} ${i:20:1} ${i:21:1} ${i:22:2} ${i:24:1} ${i:25:1} ${i:26:1}" >> $OUTPUTFILE;

done;

# CLEANUP

unset INPUTFILE;

unset OUTPUTFILE;

#

# RESULTS

#

[Tim.IT-8] ? cat example_completed.txt

123 456 789 123 456 789 12 3 4 56 7 8 9

987 654 321 987 654 321 98 7 6 54 3 2 1

231 297 301 283 120 381 29 0 3 82 1 3 3

#

# YOUR EXPECTATION

#

A B C D E F G H I J K L M

123 456 789 123 456 789 12 3 4 56 7 8 9

987 654 321 987 654 321 98 7 6 54 3 2 1

231 297 301 283 120 381 29 0 3 82 1 3 3

  • 2 months later...

Hi tim, sorry for the delayed response.

I checked your code and thanx a ton for this. It is now coming as expected. I also understood the code this time. I have a file where the lines are like

xyz12345	 12345    mprls   12345

Here the blank spaces are also character and every bit of it represents a format. e.g 1st 3 letters will represent something, 4 something else, 5-6, 7-9 and so on. It is for my own clarity, otherwise I have to debug the code manually where the debug file consists of thousands of such lines of equal width and the same format.

Thanx a lot for this. I will check this again on the raw file and if it can format the blank spaces as well.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I do trust Apple probably more than these other companies with certain data, but I also do think (and it has been demonstrated that) Apple pulls a lot of shenanigans and always has for a long time.
    • Does your iPhone support the latest iOS version? Here's the iOS 27 compatibility list by Aditya Tiwari It's that time of year when we get to know about the latest operating system updates for Apple devices. For iPhone, Apple previewed the iOS 27 update at WWDC 2026, where the company finally introduced an upgraded version of Siri. Apple typically supports iPhone models for up to five years. But it has been making exceptions in recent years (read iPhone 11). If you're wondering whether your iPhone is compatible with the iOS 27 update, here is the official list of devices: iPhone 17 Pro Max, iPhone 17 Pro, iPhone 17, iPhone 17e, iPhone Air iPhone 16 Pro Max, iPhone 16 Pro, iPhone 16, iPhone 16 Plus, iPhone 16e iPhone 15 Pro Max, iPhone 15 Pro, iPhone 15 Plus, iPhone 15 iPhone 14 Pro Max, iPhone 14 Pro, iPhone 14 Plus, iPhone 14 iPhone 13 Pro Max, iPhone 13 Pro, iPhone 13, iPhone 13 mini iPhone 12 Pro Max, iPhone 12 Pro, iPhone 12, iPhone 12 mini iPhone 11 Pro Max, iPhone 11 Pro, iPhone 11 iPhone SE (2nd generation), iPhone SE (3rd generation) So, you can download the iOS 27 developer beta on up to 31 different iPhone models. There has been no change to the list of supported iPhones since iOS 26. However, it will expand to include more devices when the iPhone 18 series arrives later this year. To download the developer beta on your iPhone, go to Settings > General > Software Update > Beta Updates. Here, select "iOS 27 Developer Beta" from the list of choices to get the new update. In addition to iOS 27, you can try the developer beta versions of macOS 27, iPadOS 27, watchOS 27, tvOS 27, and HomePod software 27 on your supported devices. iOS 27 comes with improved Liquid Glass, which you can adjust using a new transparency slider. Apple said during the keynote that iPhone apps now launch up to 30% faster, new photos appear in the Photos app up to 70% faster, and AirDrop transfers work up to 80% faster. The new update promises to improve performance on older iPhones by introducing a new CPU Scheduler that supports devices all the way back to the iPhone 11. While iOS 27 is supported on older iPhones, it goes without saying that they'll lack several features due to hardware differences. For instance, iPhone 14/14 Plus and older models come with a notch instead of the Dynamic Island. Similarly, Apple Intelligence features are supported on iPhone 15 Pro/Pro Max and later models.
    • The Radeon RX 9070 XT is right up there with the GeForce RTX 5070 Ti
    • I don't know why someone said useless, but it does have that pesky kernel driver bundled, and it's in perennial turmoil. When it goes bad, it goes very bad, and it's impossible to predict when it will due to system differences. I know that they're in the middle of development for a major new version that will include a completely new driver, one that they expect will largely solve the problem, but that's a ways out and it's unproven at this point.
    • doesn't AdGuard let ads through that pay to be let through?
  • Recent Achievements

    • Experienced
      JayZJay went up a rank
      Experienced
    • Reacting Well
      Sir_Timbit earned a badge
      Reacting Well
    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      524
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      +Edouard
      132
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!