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 totally agree no one should trust governments AND big tech companies that have near monopolies in certain market segments. The difference is of course citizens at least have some say in government. Yes, requiring Apple to open up Sirsi to other AI backends is in the interest of EU customers and certainly "opens" up the platform a crack. What are you saying? I wish the US would regulate tech companies more here and even break them up in some cases. Granted trusting the government to do it right is a crap shot at best but what is the alternative? To allow a few tech companies to continue to run everything and just print money? It's crazy to me for example that in most of the world 99% of communications go thru Facebook servers via WhatsApp. What kind of dystopian world are we living in?
    • You can.. make sure you have ADB tools on whatever OS you are running, plug your phone into your PC give it approval to talk to the phone then run this command. adb shell pm uninstall -k --user 0 com.android.chrome So long as you leave Android Webview untouched it wont break anything on your Phone to do this. I have been using exclusively Firefox mobile since it was released.
    • If it ain't broke, don't fix it. One Commander Free also available in the Store has been my goto Files Manager for years. It does what I need at a more than reasonable speed. I do occasionally use Files Explorer just because it is there and does what I need without any problems when I use it.
    • I think you missed his point. He wasn't saying that cloud storage isn't possible on GNU/Linux, clearly it is. He was pointing out that you are commenting about your dislike of Windows on an article about OneDrive (not Windows) for Mac (also not Windows). Its about as off topic as coming to an article about Sony improving something on the PlayStation and saying that you hate Sony TVs and prefer LG.
    • Nvidia's GeForce NOW summer sale drops prices for Ultimate and Premium memberships by Pulasthi Ariyasinghe Nvidia has a fresh update for GeForce NOW subscribers today, bringing in more games to add to its ever-growing supported titles list. At the same time, the company announced the kick-off for its summer sale for the streaming subscription service, dropping the prices for both its premium packages for anyone looking to upgrade or join. The offer is for the 12-month membership options that the company offers. This drops the 12-month Performance membership from $99.99 to $64.99, saving members $35. Next, the 12-month Ultimate membership is currently going for $129.99, dropping prices by $70 from the original $199.99. "The Performance membership delivers smooth, high-quality cloud gaming across devices, with streaming up to 1080p at 60 frames per second (fps) and access to RTX-powered servers for supported games," says Nvidia, describing its tiers. "The Ultimate membership steps things up with RTX 4080‑ or 5080‑class performance in the cloud, supporting up to 4K and beyond on ultrawide displays, up to 120 fps, and advanced features like ray tracing, NVIDIA DLSS and NVIDIA Reflex for a more responsive, visually rich experience." With the sales out of the way, here are the games joining GeForce NOW's supported list this week: NBA THE RUN (New release on Steam, available on June 9) Witchspire (New release on Steam, available on June 10) SpaceCraft (New release on Steam, available on June 11) Duet Night Abyss (Launcher) DOOM Eternal (Epic Games Store) The Elder Scrolls Online (Xbox, available on Game Pass) Farever (Steam) World of Tanks: HEAT (Wargaming) Nvidia plans to add support for a bunch of more games during the rest of June. Find the full announcement from last week over here. Keep in mind that, unlike subscription services like Game Pass or EA Play, a copy of a game must be owned by the GeForce NOW member (or at least have a license via PC Game Pass) to start playing via Nvidia's cloud servers. There is also a limit to how many hours subscribers can use the service per month.
  • Recent Achievements

    • One Month Later
      Tommi118 earned a badge
      One Month Later
    • One Month Later
      sjbousquet earned a badge
      One Month Later
    • Week One Done
      sjbousquet earned a badge
      Week One Done
    • First Post
      DragonOfMercy earned a badge
      First Post
    • First Post
      bella52 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      501
    2. 2
      PsYcHoKiLLa
      208
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      83
    5. 5
      FloatingFatMan
      73
  • Tell a friend

    Love Neowin? Tell a friend!