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

  Quote

123456789123456789123456789

987654321987654321987654321

231297301283120381290382133

Now I need script to format this file like

  Quote

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 :)

  On 14/02/2013 at 03:38, mediator said:

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

  Quote

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

    • Gemini CLI brings AI smarts to your Firebase terminal experience by Paul Hill Google has added its Gemini AI model directly into the command-line interface of Firebase Studio, its cloud-based IDE that uses AI to help with projects. The Gemini Command Line Interface (CLI) means that developers can expand past using AI for code, they can now also use AI for content generation and research without leaving the IDE. Gemini CLI comes with free usage tiers (60 model requests per minute, 1,000 requests per day with a Google login), it offers advanced AI features, and includes integrated Google Search for real-time content. Gemini CLI is also open source so it can be customized and accepts contributions. Accessing the Gemini CLI within Firebase Studio is straightforward, just press “Code view” in the top-right. From there, open up the terminal from the burger menu then select Terminal and New Terminal. Then in the terminal, type gemini and go through the setup, you can just press enter twice to get started. Out of the box, you’ll be able to get started with Gemini 2.5 Pro by just typing a query and pressing enter. There is also a non-interactive mode that’s useful for scripting and automation. To use it you use the –prompt or -p flags followed by your query wrapped in quotes, for example: gemini -p “What is the capital of France?” In this mode, Gemini CLI automatically closes after completing the request. During setup, there was the option to choose a theme. If you ever want to change it or look at other settings such as usage states, tool access, or checkpointing, you can edit them via .gemini/settings.json. You can also add API keys or choose different models in .env and you can using GEMINI.md to provide project-specific context, instructions, and coding styles in Gemini for a more tailored response. With Gemini CLI, you can have it explain code, refactor code, debug errors, and summarize information. It’s as simple as typing explain [file], refactor , debug “Error message”, or summarize “topic”. There are also built-in commands for managing the session such as /help for a command list, /chat to save and resume conversations, /tools to see available tools, and /restore to undo tool-made file edits. Firebase Studio, in true Google fashion, is a cloud-based IDE used in your web browser, making it excellent for weaker computers. To get started, you can head to the Firebase website. From there, tap Studio in the top-right.
    • I would prefer local ai over online ai on some tasks . They are good enough for tasks like artificial voice , image editing , text corrections , tagging etc . Local AI on Windows Photo editor is impressive for example . There are probably many other Ai that we could run on simple pc with GPUs .
    • oh look, we are under the control of the U.S again, is it not about time we as a country did this ourselves if they want it, not rely on greedy U.S. tech companies? We are capable of doing so as a country. I hate how the U.K have gone with no industry like we used to have and relying on other countries. Maybe we should follow Trump words, and make Britain great again and stop bending over to please Trump and other countries. Our government is Trumps puppet. I know we need to co-operate on some things, but come on.
    • Actually I see no problem is notifying readers in advance. The post author mentioned that it'll be available on July 22.
  • Recent Achievements

    • Week One Done
      MIghty Haul earned a badge
      Week One Done
    • One Month Later
      MIghty Haul earned a badge
      One Month Later
    • Collaborator
      KD2004 earned a badge
      Collaborator
    • One Month Later
      ataho31016 earned a badge
      One Month Later
    • One Month Later
      Delahenty Machinery earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      592
    2. 2
      Michael Scrip
      200
    3. 3
      ATLien_0
      192
    4. 4
      +FloatingFatMan
      140
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!